| 1 |
<?php |
| 2 |
/** |
| 3 |
* Campaigns widget class. |
| 4 |
* |
| 5 |
* @class Charitable_Campaigns_Widget |
| 6 |
* @version 1.0.0 |
| 7 |
* @package Charitable/Widgets/Campaigns Widget |
| 8 |
* @category Class |
| 9 |
* @author David Bisset |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Campaigns_Widget' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Campaigns_Widget class. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
class Charitable_Campaigns_Widget extends WP_Widget { |
| 25 |
|
| 26 |
/** |
| 27 |
* Instantiate the widget and set up basic configuration. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
parent::__construct( |
| 33 |
'charitable_campaigns_widget', |
| 34 |
__( 'Campaigns', 'charitable' ), |
| 35 |
array( |
| 36 |
'description' => __( 'Displays your Charitable campaigns.', 'charitable' ), |
| 37 |
'customize_selective_refresh' => true, |
| 38 |
) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Display the widget contents on the front-end. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* |
| 47 |
* @param array $args |
| 48 |
* @param array $instance |
| 49 |
*/ |
| 50 |
public function widget( $args, $instance ) { |
| 51 |
$view_args = array_merge( $args, $instance ); |
| 52 |
$view_args['campaigns'] = $this->get_widget_campaigns( $instance ); |
| 53 |
|
| 54 |
charitable_template( 'widgets/campaigns.php', $view_args ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Display the widget form in the admin. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* |
| 62 |
* @param array $instance The current settings for the widget options. |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function form( $instance ) { |
| 66 |
$defaults = array( |
| 67 |
'title' => '', |
| 68 |
'number' => 10, |
| 69 |
'order' => 'recent', |
| 70 |
'show_thumbnail' => false, |
| 71 |
); |
| 72 |
|
| 73 |
$args = wp_parse_args( $instance, $defaults ); |
| 74 |
?> |
| 75 |
<p> |
| 76 |
<label for="<?php echo $this->get_field_id( 'title' ) ?>"><?php _e( 'Title:', 'charitable' ) ?></label> |
| 77 |
<input type="text" name="<?php echo $this->get_field_name( 'title' ) ?>" id="<?php echo $this->get_field_id( 'title' ) ?>" value="<?php echo $args['title'] ?>"/> |
| 78 |
</p> |
| 79 |
<p> |
| 80 |
<label for="<?php echo $this->get_field_id( 'number' ) ?>"><?php _e( 'Number of campaigns to display:', 'charitable' ) ?></label> |
| 81 |
<input type="text" name="<?php echo $this->get_field_name( 'number' ) ?>" id="<?php echo $this->get_field_id( 'number' ) ?>" value="<?php echo $args['number'] ?>" /> |
| 82 |
</p> |
| 83 |
<p> |
| 84 |
<input id="<?php echo esc_attr( $this->get_field_id( 'show_thumbnail' ) ) ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_thumbnail' ) ); ?>" <?php checked( $args['show_thumbnail'] ) ?>> |
| 85 |
<label for="<?php echo esc_attr( $this->get_field_id( 'show_thumbnail' ) ) ?>"><?php _e( 'Show thumbnail', 'charitable' ) ?></label> |
| 86 |
</p> |
| 87 |
<p> |
| 88 |
<label for="<?php echo $this->get_field_id( 'order' ) ?>"><?php _e( 'Order:', 'charitable' ) ?></label> |
| 89 |
<select name="<?php echo $this->get_field_name( 'order' ) ?>" id="<?php echo $this->get_field_id( 'order' ) ?>"> |
| 90 |
<option value="recent" <?php selected( 'recent', $args['order'] ) ?>><?php _e( 'Date published', 'charitable' ) ?></option> |
| 91 |
<option value="ending" <?php selected( 'ending', $args['order'] ) ?>><?php _e( 'Ending soonest', 'charitable' ) ?></option> |
| 92 |
</select> |
| 93 |
</p> |
| 94 |
<?php |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Update the widget settings in the admin. |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* |
| 102 |
* @param array $new_instance The updated settings. |
| 103 |
* @param array $new_instance The old settings. |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function update( $new_instance, $old_instance ) { |
| 107 |
|
| 108 |
$instance = array(); |
| 109 |
$instance['title'] = isset( $new_instance['title'] ) ? $new_instance['title'] : $old_instance['title']; |
| 110 |
$instance['number'] = isset( $new_instance['number'] ) ? $new_instance['number'] : $old_instance['number']; |
| 111 |
$instance['show_thumbnail'] = isset( $new_instance['show_thumbnail'] ) && $new_instance['show_thumbnail']; |
| 112 |
$instance['order'] = isset( $new_instance['order'] ) ? $new_instance['order'] : $old_instance['order']; |
| 113 |
return $instance; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Return campaigns to display in the widget. |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @param array $instance |
| 122 |
* @return WP_Query |
| 123 |
*/ |
| 124 |
protected function get_widget_campaigns( $instance ) { |
| 125 |
|
| 126 |
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; |
| 127 |
$args = array( |
| 128 |
'posts_per_page' => $number, |
| 129 |
); |
| 130 |
|
| 131 |
if ( isset( $instance['order'] ) && 'recent' == $instance['order'] ) { |
| 132 |
$args['orderby'] = 'date'; |
| 133 |
$args['order'] = 'DESC'; |
| 134 |
return Charitable_Campaigns::query( $args ); |
| 135 |
} |
| 136 |
|
| 137 |
return Charitable_Campaigns::ordered_by_ending_soon( $args ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
endif; |
| 142 |
|