|
| 1 | +<?php |
| 2 | +/* |
| 3 | +Plugin Name: Chuffed Donation Widget |
| 4 | +Plugin URI: http://ignite.digitalignition.net/articlesexamples/chuffed-donation-widget |
| 5 | +Description: Easily add a widget for your chuffed campaign |
| 6 | +Author: Greg Tangey |
| 7 | +Author URI: http://ignite.digitalignition.net/ |
| 8 | +Version: 0.1 |
| 9 | +*/ |
| 10 | + |
| 11 | +/* Copyright 2015 Greg Tangey (email : [email protected]) |
| 12 | +
|
| 13 | + This program is free software; you can redistribute it and/or modify |
| 14 | + it under the terms of the GNU General Public License as published by |
| 15 | + the Free Software Foundation; either version 2 of the License, or |
| 16 | + (at your option) any later version. |
| 17 | +
|
| 18 | + This program is distributed in the hope that it will be useful, |
| 19 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + GNU General Public License for more details. |
| 22 | +
|
| 23 | + You should have received a copy of the GNU General Public License |
| 24 | + along with this program; if not, write to the Free Software |
| 25 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | +*/ |
| 27 | + |
| 28 | +class ChuffedWidget extends WP_Widget { |
| 29 | + |
| 30 | + /** |
| 31 | + * Register widget with WordPress. |
| 32 | + */ |
| 33 | + function __construct() { |
| 34 | + parent::__construct( |
| 35 | + 'chuffed_widget', // Base ID |
| 36 | + __( 'Chuffed Campaign', 'text_domain' ), // Name |
| 37 | + array( 'description' => __( 'A chuffed.org campaign widget', 'text_domain' ), ) // Args |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Front-end display of widget. |
| 43 | + * |
| 44 | + * @see WP_Widget::widget() |
| 45 | + * |
| 46 | + * @param array $args Widget arguments. |
| 47 | + * @param array $instance Saved values from database. |
| 48 | + */ |
| 49 | + public function widget( $args, $instance ) { |
| 50 | + $campaign_id = $instance['campaign_id']; |
| 51 | + |
| 52 | + if( ! empty($campaign_id) ) { |
| 53 | + $transName = "chuffed-widget-$campaign_id"; |
| 54 | + $cacheTime = 30; // minutes |
| 55 | + // delete_transient($transName); |
| 56 | + if(false === ($chuffedData = get_transient($transName) ) ){ |
| 57 | + $json = wp_remote_get("http://chuffed.org/api/v1/campaign/$campaign_id"); |
| 58 | + |
| 59 | + // Check the response code |
| 60 | + $response_code = wp_remote_retrieve_response_code( $json ); |
| 61 | + $response_message = wp_remote_retrieve_response_message( $json ); |
| 62 | + |
| 63 | + |
| 64 | + if ( 200 != $response_code && ! empty( $response_message ) ) { |
| 65 | + $err = $response_message; |
| 66 | + } elseif ( 200 != $response_code ) { |
| 67 | + $err = "Uknown err"; |
| 68 | + } else { |
| 69 | + $chuffedData = wp_remote_retrieve_body( $json ); |
| 70 | + } |
| 71 | + |
| 72 | + $chuffedData = json_decode($chuffedData, true); |
| 73 | + set_transient($transName, $chuffedData, 60 * $cacheTime); |
| 74 | + } |
| 75 | + $targetAmount = intval($chuffedData['data']['camp_amount']); |
| 76 | + $collectedAmount = intval($chuffedData['data']['camp_amount_collected']); |
| 77 | + $percWidth = intval(($collectedAmount/$targetAmount)*100); |
| 78 | + $slug = $chuffedData['data']['slug']; |
| 79 | + $title = $chuffedData['data']['title']; |
| 80 | + |
| 81 | + echo $args['before_widget']; |
| 82 | + if ( ! empty( $instance['title'] ) ) { |
| 83 | + echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title']; |
| 84 | + } |
| 85 | + ?> |
| 86 | + <a style="text-decoration: none" href="https://chuffed.org/project/<?php echo $slug; ?>"> |
| 87 | + <div style="position:relative;"> |
| 88 | + <h1><?php echo $title; ?></h1> |
| 89 | + <div style="width: 100%;height:15px;background-color: #F9F9F9 !important;"> |
| 90 | + <div style="width: <?php echo $percWidth;?>%; height: 15px;background-color: #28ab60 !important;"></div> |
| 91 | + </div> |
| 92 | + <h2 style="font-size: 50px;margin-bottom: 0;padding-bottom: 0;line-height: 56px;"> |
| 93 | + $<span><?php echo $collectedAmount; ?></span> |
| 94 | + </h2> |
| 95 | + <p style="color:#9b9b9b;"><?php echo __("Raised of", "text_domain"); ?> |
| 96 | + $<span><?php echo $targetAmount; ?></span> |
| 97 | + </p> |
| 98 | + </div> |
| 99 | + </a> |
| 100 | + <?php |
| 101 | + |
| 102 | + echo $args['after_widget']; |
| 103 | + } |
| 104 | + else { |
| 105 | + echo __("A Campaign ID is not set in the widgets setting", "text_domain"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Back-end widget form. |
| 111 | + * |
| 112 | + * @see WP_Widget::form() |
| 113 | + * |
| 114 | + * @param array $instance Previously saved values from database. |
| 115 | + */ |
| 116 | + public function form( $instance ) { |
| 117 | + $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Chuffed Campaign', 'text_domain' ); |
| 118 | + $campaign_id = ! empty( $instance['campaign_id'] ) ? $instance['campaign_id'] : ''; |
| 119 | + |
| 120 | + ?> |
| 121 | + <p> |
| 122 | + <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
| 123 | + <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> |
| 124 | + <label for="<?php echo $this->get_field_id( 'campaign_id' ); ?>"><?php _e( 'Campaign ID:' ); ?></label> |
| 125 | + <input class="widefat" id="<?php echo $this->get_field_id( 'campaign_id' ); ?>" name="<?php echo $this->get_field_name( 'campaign_id' ); ?>" type="text" value="<?php echo esc_attr( $campaign_id ); ?>"> |
| 126 | + </p> |
| 127 | + <?php |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Sanitize widget form values as they are saved. |
| 132 | + * |
| 133 | + * @see WP_Widget::update() |
| 134 | + * |
| 135 | + * @param array $new_instance Values just sent to be saved. |
| 136 | + * @param array $old_instance Previously saved values from database. |
| 137 | + * |
| 138 | + * @return array Updated safe values to be saved. |
| 139 | + */ |
| 140 | + public function update( $new_instance, $old_instance ) { |
| 141 | + $instance = array(); |
| 142 | + $instance['campaign_id'] = ( ! empty( $new_instance['campaign_id'] ) ) ? strip_tags( $new_instance['campaign_id'] ) : ''; |
| 143 | + $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 144 | + |
| 145 | + return $instance; |
| 146 | + } |
| 147 | + |
| 148 | +} |
| 149 | + |
| 150 | +function register_chuffed_widget() { |
| 151 | + register_widget( 'ChuffedWidget' ); |
| 152 | +} |
| 153 | +add_action( 'widgets_init', 'register_chuffed_widget' ); |
0 commit comments