| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sets up the campaign list table in the admin. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Campaign_List_Table |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.5.0 |
| 10 |
* @version 1.6.45 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Campaign_List_Table' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Campaign_List_Table class. |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
final class Charitable_Campaign_List_Table { |
| 25 |
|
| 26 |
/** |
| 27 |
* The single instance of this class. |
| 28 |
* |
| 29 |
* @since 1.5.0 |
| 30 |
* |
| 31 |
* @var Charitable_Campaign_List_Table|null |
| 32 |
*/ |
| 33 |
private static $instance = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns and/or create the single instance of this class. |
| 37 |
* |
| 38 |
* @since 1.5.0 |
| 39 |
* |
| 40 |
* @return Charitable_Campaign_List_Table |
| 41 |
*/ |
| 42 |
public static function get_instance() { |
| 43 |
if ( is_null( self::$instance ) ) { |
| 44 |
self::$instance = new self(); |
| 45 |
} |
| 46 |
|
| 47 |
return self::$instance; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Customize campaigns columns. |
| 52 |
* |
| 53 |
* @see get_column_headers |
| 54 |
* |
| 55 |
* @since 1.5.0 |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public function dashboard_columns() { |
| 60 |
/** |
| 61 |
* Filter the columns shown in the campaigns list table. |
| 62 |
* |
| 63 |
* @since 1.5.0 |
| 64 |
* |
| 65 |
* @param array $columns The list of columns. |
| 66 |
*/ |
| 67 |
return apply_filters( |
| 68 |
'charitable_campaign_dashboard_column_names', |
| 69 |
array( |
| 70 |
'cb' => '<input type="checkbox"/>', |
| 71 |
'ID' => __( '#', 'charitable' ), |
| 72 |
'title' => __( 'Title', 'charitable' ), |
| 73 |
'author' => __( 'Creator', 'charitable' ), |
| 74 |
'donated' => __( 'Donations', 'charitable' ), |
| 75 |
'end_date' => __( 'End Date', 'charitable' ), |
| 76 |
'status' => __( 'Status', 'charitable' ), |
| 77 |
'date' => __( 'Date Created', 'charitable' ), |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Add information to the dashboard campaign table listing. |
| 84 |
* |
| 85 |
* @see WP_Posts_List_Table::single_row() |
| 86 |
* |
| 87 |
* @since 1.5.0 |
| 88 |
* |
| 89 |
* @param string $column_name The name of the column to display. |
| 90 |
* @param int $post_id The current post ID. |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function dashboard_column_item( $column_name, $post_id ) { |
| 94 |
$campaign = charitable_get_campaign( $post_id ); |
| 95 |
|
| 96 |
switch ( $column_name ) { |
| 97 |
case 'ID': |
| 98 |
$display = $post_id; |
| 99 |
break; |
| 100 |
|
| 101 |
case 'donated': |
| 102 |
$display = charitable_format_money( $campaign->get_donated_amount() ); |
| 103 |
break; |
| 104 |
|
| 105 |
case 'end_date': |
| 106 |
$display = $campaign->is_endless() ? '∞' : $campaign->get_end_date(); |
| 107 |
break; |
| 108 |
|
| 109 |
case 'status': |
| 110 |
$status = $campaign->get_status(); |
| 111 |
|
| 112 |
if ( 'finished' == $status && $campaign->has_goal() ) { |
| 113 |
$status = $campaign->has_achieved_goal() ? 'successful' : 'unsuccessful'; |
| 114 |
} |
| 115 |
|
| 116 |
$display = '<mark class="' . esc_attr( $status ) . '">' . $status . '</mark>'; |
| 117 |
break; |
| 118 |
|
| 119 |
default: |
| 120 |
$display = ''; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Filter the output of the cell. |
| 125 |
* |
| 126 |
* @since 1.5.0 |
| 127 |
* |
| 128 |
* @param string $display The content that will be displayed. |
| 129 |
* @param string $column_name The name of the column. |
| 130 |
* @param int $post_id The ID of the campaign being shown. |
| 131 |
* @param Charitable_Campaign $campaign The Charitable_Campaign object. |
| 132 |
*/ |
| 133 |
echo apply_filters( 'charitable_campaign_column_display', $display, $column_name, $post_id, $campaign ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Modify bulk messages |
| 138 |
* |
| 139 |
* @since 1.5.0 |
| 140 |
* |
| 141 |
* @param array $bulk_messages Messages to show after bulk actions. |
| 142 |
* @param array $bulk_counts Array showing how many items were affected by the action. |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
public function bulk_messages( $bulk_messages, $bulk_counts ) { |
| 146 |
$bulk_messages[ Charitable::CAMPAIGN_POST_TYPE ] = array( |
| 147 |
'updated' => _n( '%d campaign updated.', '%d campaigns updated.', $bulk_counts['updated'], 'charitable' ), |
| 148 |
'locked' => ( 1 == $bulk_counts['locked'] ) |
| 149 |
? __( '1 campaign not updated, somebody is editing it.' ) |
| 150 |
: _n( '%s campaign not updated, somebody is editing it.', '%s campaigns not updated, somebody is editing them.', $bulk_counts['locked'], 'charitable' ), |
| 151 |
'deleted' => _n( '%s campaign permanently deleted.', '%s campaigns permanently deleted.', $bulk_counts['deleted'], 'charitable' ), |
| 152 |
'trashed' => _n( '%s campaign moved to the Trash.', '%s campaigns moved to the Trash.', $bulk_counts['trashed'], 'charitable' ), |
| 153 |
'untrashed' => _n( '%s campaign restored from the Trash.', '%s campaigns restored from the Trash.', $bulk_counts['untrashed'], 'charitable' ), |
| 154 |
); |
| 155 |
|
| 156 |
return $bulk_messages; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Add extra buttons after filters |
| 161 |
* |
| 162 |
* @since 1.6.0 |
| 163 |
* |
| 164 |
* @param string $which The context where this is being called. |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function add_export( $which ) { |
| 168 |
if ( ! current_user_can( 'export_charitable_reports' ) ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
if ( 'top' === $which && $this->is_campaigns_page() ) { |
| 173 |
charitable_admin_view( 'campaigns-page/export' ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Add filters above the campaigns table. |
| 179 |
* |
| 180 |
* @since 1.6.36 |
| 181 |
* |
| 182 |
* @global string $typenow The current post type. |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function add_filters() { |
| 187 |
global $typenow; |
| 188 |
|
| 189 |
/* Show custom filters to filter orders by donor. */ |
| 190 |
if ( in_array( $typenow, array( Charitable::CAMPAIGN_POST_TYPE ) ) ) { |
| 191 |
charitable_admin_view( 'campaigns-page/filters' ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Add modal template to footer. |
| 197 |
* |
| 198 |
* @since 1.6.0 |
| 199 |
* |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
public function modal_forms() { |
| 203 |
if ( $this->is_campaigns_page() ) { |
| 204 |
charitable_admin_view( 'campaigns-page/filter-form' ); |
| 205 |
charitable_admin_view( 'campaigns-page/export-form' ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Admin scripts and styles. |
| 211 |
* |
| 212 |
* Set up the scripts & styles used for the modal. |
| 213 |
* |
| 214 |
* @since 1.6.0 |
| 215 |
* |
| 216 |
* @param string $hook The current page hook/slug. |
| 217 |
* @return void |
| 218 |
*/ |
| 219 |
public function load_scripts( $hook ) { |
| 220 |
if ( 'edit.php' != $hook ) { |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
if ( $this->is_campaigns_page() ) { |
| 225 |
wp_enqueue_style( 'lean-modal-css' ); |
| 226 |
wp_enqueue_script( 'jquery' ); |
| 227 |
wp_enqueue_script( 'lean-modal' ); |
| 228 |
wp_enqueue_script( 'charitable-admin-tables' ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Add custom filters to the query that returns the campaigns to be displayed. |
| 234 |
* |
| 235 |
* @since 1.6.36 |
| 236 |
* |
| 237 |
* @global string $typenow The current post type. |
| 238 |
* |
| 239 |
* @param array $vars The array of args to pass to WP_Query. |
| 240 |
* @return array |
| 241 |
*/ |
| 242 |
public function filter_request_query( $vars ) { |
| 243 |
if ( ! $this->is_campaigns_page() ) { |
| 244 |
return $vars; |
| 245 |
} |
| 246 |
|
| 247 |
/* No Status: fix WP's crappy handling of "all" post status. */ |
| 248 |
if ( ! isset( $_GET['post_status'] ) || empty( $_GET['post_status'] ) || 'all' === $_GET['post_status'] ) { |
| 249 |
$vars['post_status'] = array_keys( get_post_statuses() ); |
| 250 |
$vars['perm'] = 'readable'; |
| 251 |
} else { |
| 252 |
switch ( $_GET['post_status'] ) { |
| 253 |
case 'active': |
| 254 |
$vars['post_status'] = 'publish'; |
| 255 |
$vars['meta_query'] = array( |
| 256 |
'relation' => 'OR', |
| 257 |
array( |
| 258 |
'key' => '_campaign_end_date', |
| 259 |
'value' => date( 'Y-m-d H:i:s' ), |
| 260 |
'compare' => '>', |
| 261 |
'type' => 'datetime', |
| 262 |
), |
| 263 |
array( |
| 264 |
'key' => '_campaign_end_date', |
| 265 |
'value' => 0, |
| 266 |
'compare' => '=', |
| 267 |
), |
| 268 |
); |
| 269 |
break; |
| 270 |
|
| 271 |
case 'finish': |
| 272 |
$vars['post_status'] = 'publish'; |
| 273 |
$vars['meta_query'] = array( |
| 274 |
array( |
| 275 |
'key' => '_campaign_end_date', |
| 276 |
'value' => date( 'Y-m-d H:i:s' ), |
| 277 |
'compare' => '<=', |
| 278 |
'type' => 'datetime', |
| 279 |
), |
| 280 |
); |
| 281 |
break; |
| 282 |
|
| 283 |
default: |
| 284 |
$vars['post_status'] = $_GET['post_status']; |
| 285 |
$vars['perm'] = 'readable'; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if ( ! empty( filter_input( INPUT_GET, 'charitable_nonce', FILTER_UNSAFE_RAW ) ) && |
| 290 |
wp_verify_nonce( filter_input( INPUT_GET, 'charitable_nonce', FILTER_UNSAFE_RAW ), 'charitable_filter_campaigns' ) ) : |
| 291 |
|
| 292 |
/* Set up start date query */ |
| 293 |
if ( isset( $_GET['start_date_from'] ) && ! empty( filter_input( INPUT_GET, 'start_date_from', FILTER_UNSAFE_RAW ) ) ) { |
| 294 |
$start_date_from = $this->get_parsed_date( filter_input( INPUT_GET, 'start_date_from', FILTER_UNSAFE_RAW ) ); |
| 295 |
$vars['date_query']['after'] = array( |
| 296 |
'year' => $start_date_from['year'], |
| 297 |
'month' => $start_date_from['month'], |
| 298 |
'day' => $start_date_from['day'], |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
if ( isset( $_GET['start_date_to'] ) && ! empty( filter_input( INPUT_GET, 'start_date_to', FILTER_UNSAFE_RAW ) ) ) { |
| 303 |
$start_date_to = $this->get_parsed_date( filter_input( INPUT_GET, 'start_date_to', FILTER_UNSAFE_RAW ) ); |
| 304 |
$vars['date_query']['before'] = array( |
| 305 |
'year' => $start_date_to['year'], |
| 306 |
'month' => $start_date_to['month'], |
| 307 |
'day' => $start_date_to['day'], |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
/* Set up end date query */ |
| 312 |
if ( isset( $_GET['end_date_from'] ) && ! empty( filter_input( INPUT_GET, 'end_date_from', FILTER_UNSAFE_RAW ) ) ) { |
| 313 |
$end_date_from = $this->get_parsed_date( filter_input( INPUT_GET, 'end_date_from', FILTER_UNSAFE_RAW ) ); |
| 314 |
$vars['meta_query'][] = array( |
| 315 |
'key' => '_campaign_end_date', |
| 316 |
'value' => sprintf( '%d-%d-%d 00:00:00', $end_date_from['year'], $end_date_from['month'], $end_date_from['day'] ), |
| 317 |
'compare' => '>=', |
| 318 |
'type' => 'datetime', |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
if ( isset( $_GET['end_date_to'] ) && ! empty( filter_input( INPUT_GET, 'end_date_to', FILTER_UNSAFE_RAW ) ) ) { |
| 323 |
$end_date_to = $this->get_parsed_date( filter_input( INPUT_GET, 'end_date_to', FILTER_UNSAFE_RAW ) ); |
| 324 |
$vars['meta_query'][] = array( |
| 325 |
'key' => '_campaign_end_date', |
| 326 |
'value' => sprintf( '%d-%d-%d 00:00:00', $end_date_to['year'], $end_date_to['month'], $end_date_to['day'] ), |
| 327 |
'compare' => '<=', |
| 328 |
'type' => 'datetime', |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
if ( isset( $vars['date_query'] ) ) { |
| 333 |
$vars['date_query']['inclusive'] = true; |
| 334 |
} |
| 335 |
|
| 336 |
/* Filter by campaign. */ |
| 337 |
if ( isset( $_GET['campaign_id'] ) && false !== stripos( filter_input( INPUT_GET, 'campaign_id', FILTER_UNSAFE_RAW ), 'all' ) ) { |
| 338 |
$vars['post__in'] = charitable_get_table( 'campaign_donations' )->get_donation_ids_for_campaign( $_GET['campaign_id'] ); |
| 339 |
} |
| 340 |
|
| 341 |
endif; |
| 342 |
|
| 343 |
/* Restrict by author if user can only edit their own. */ |
| 344 |
if ( ! current_user_can( 'edit_others_campaigns' ) ) { |
| 345 |
$vars['author'] = get_current_user_id(); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Filter the campaign list query vars. |
| 350 |
* |
| 351 |
* @since 1.6.36 |
| 352 |
* |
| 353 |
* @param array $vars The request query vars. |
| 354 |
*/ |
| 355 |
return apply_filters( 'charitable_filter_campaigns_list_request_vars', $vars ); |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Given a date, returns an array containing the date, month and year. |
| 360 |
* Updated in 1.7.0.8 to reflect a change in date format, but points to a new date format function in case someone else is using the old one. |
| 361 |
* |
| 362 |
* @since 1.6.36 |
| 363 |
* |
| 364 |
* @param string $date A date as a string that can be parsed by strtotime. |
| 365 |
* @return string[] |
| 366 |
*/ |
| 367 |
protected function get_parsed_date( $date ) { |
| 368 |
$time = charitable_sanitize_date_alt_format( $date ); |
| 369 |
|
| 370 |
$args = array( |
| 371 |
'year' => date( 'Y', $time ), |
| 372 |
'month' => date( 'm', $time ), |
| 373 |
'day' => date( 'd', $time ), |
| 374 |
); |
| 375 |
|
| 376 |
return $args; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Checks whether this is the campaigns page. |
| 381 |
* |
| 382 |
* @since 1.6.0 |
| 383 |
* |
| 384 |
* @global string $typenow The current post type. |
| 385 |
* @global string $pagenow The current admin page. |
| 386 |
* |
| 387 |
* @return boolean |
| 388 |
*/ |
| 389 |
private function is_campaigns_page() { |
| 390 |
global $typenow, $pagenow; |
| 391 |
|
| 392 |
return 'edit.php' === $pagenow && in_array( $typenow, array( Charitable::CAMPAIGN_POST_TYPE ), true ); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
endif; |
| 397 |
|