dashboard-columns.php
366 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dashboard Columns |
| 4 | * |
| 5 | * @package GIVE |
| 6 | * @subpackage Admin/Forms |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * Give Forms Columns |
| 20 | * |
| 21 | * Defines the custom columns and their order |
| 22 | * |
| 23 | * @since 1.0 |
| 24 | * |
| 25 | * @param array $give_form_columns Array of forms columns |
| 26 | * |
| 27 | * @return array $form_columns Updated array of forms columns |
| 28 | * Post Type List Table |
| 29 | */ |
| 30 | function give_form_columns( $give_form_columns ) { |
| 31 | |
| 32 | // Standard columns |
| 33 | $give_form_columns = array( |
| 34 | 'cb' => '<input type="checkbox"/>', |
| 35 | 'title' => __( 'Name', 'give' ), |
| 36 | 'form_category' => __( 'Categories', 'give' ), |
| 37 | 'form_tag' => __( 'Tags', 'give' ), |
| 38 | 'price' => __( 'Amount', 'give' ), |
| 39 | 'goal' => __( 'Goal', 'give' ), |
| 40 | 'donations' => __( 'Donations', 'give' ), |
| 41 | 'earnings' => __( 'Income', 'give' ), |
| 42 | 'shortcode' => __( 'Shortcode', 'give' ), |
| 43 | 'date' => __( 'Date', 'give' ), |
| 44 | ); |
| 45 | |
| 46 | // Does the user want categories / tags? |
| 47 | if ( ! give_is_setting_enabled( give_get_option( 'categories', 'disabled' ) ) ) { |
| 48 | unset( $give_form_columns['form_category'] ); |
| 49 | } |
| 50 | if ( ! give_is_setting_enabled( give_get_option( 'tags', 'disabled' ) ) ) { |
| 51 | unset( $give_form_columns['form_tag'] ); |
| 52 | } |
| 53 | |
| 54 | return apply_filters( 'give_forms_columns', $give_form_columns ); |
| 55 | } |
| 56 | |
| 57 | add_filter( 'manage_edit-give_forms_columns', 'give_form_columns' ); |
| 58 | |
| 59 | /** |
| 60 | * Render Give Form Columns |
| 61 | * |
| 62 | * @since 1.0 |
| 63 | * |
| 64 | * @param string $column_name Column name |
| 65 | * @param int $post_id Give Form (Post) ID |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | function give_render_form_columns( $column_name, $post_id ) { |
| 70 | if ( get_post_type( $post_id ) == 'give_forms' ) { |
| 71 | |
| 72 | switch ( $column_name ) { |
| 73 | case 'form_category': |
| 74 | echo get_the_term_list( $post_id, 'give_forms_category', '', ', ', '' ); |
| 75 | break; |
| 76 | case 'form_tag': |
| 77 | echo get_the_term_list( $post_id, 'give_forms_tag', '', ', ', '' ); |
| 78 | break; |
| 79 | case 'price': |
| 80 | if ( give_has_variable_prices( $post_id ) ) { |
| 81 | echo give_price_range( $post_id ); |
| 82 | } else { |
| 83 | echo give_price( $post_id, false ); |
| 84 | printf( '<input type="hidden" class="formprice-%1$s" value="%2$s" />', esc_attr( $post_id ), esc_attr( give_get_form_price( $post_id ) ) ); |
| 85 | } |
| 86 | break; |
| 87 | case 'goal': |
| 88 | if ( give_is_setting_enabled( give_get_meta( $post_id, '_give_goal_option', true ) ) ) { |
| 89 | |
| 90 | echo give_admin_form_goal_stats( $post_id ); |
| 91 | |
| 92 | } else { |
| 93 | _e( 'No Goal Set', 'give' ); |
| 94 | } |
| 95 | |
| 96 | printf( |
| 97 | '<input type="hidden" class="formgoal-%1$s" value="%2$s" />', |
| 98 | esc_attr( $post_id ), |
| 99 | give_get_form_goal( $post_id ) |
| 100 | ); |
| 101 | |
| 102 | break; |
| 103 | case 'donations': |
| 104 | if ( current_user_can( 'view_give_form_stats', $post_id ) ) { |
| 105 | printf( |
| 106 | '<a href="%1$s">%2$s</a>', |
| 107 | esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&form_id=' . $post_id ) ), |
| 108 | give_get_form_sales_stats( $post_id ) |
| 109 | ); |
| 110 | } else { |
| 111 | echo '-'; |
| 112 | } |
| 113 | break; |
| 114 | case 'earnings': |
| 115 | if ( current_user_can( 'view_give_form_stats', $post_id ) ) { |
| 116 | printf( |
| 117 | '<a href="%1$s">%2$s</a>', |
| 118 | esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-reports&tab=forms&form-id=' . $post_id ) ), |
| 119 | give_currency_filter( give_format_amount( give_get_form_earnings_stats( $post_id ), array( 'sanitize' => false ) ) ) |
| 120 | ); |
| 121 | } else { |
| 122 | echo '-'; |
| 123 | } |
| 124 | break; |
| 125 | case 'shortcode': |
| 126 | printf( '<input onclick="this.setSelectionRange(0, this.value.length)" type="text" class="shortcode-input" readonly="" value="[give_form id="%s"]"', absint( $post_id ) ); |
| 127 | break; |
| 128 | }// End switch(). |
| 129 | }// End if(). |
| 130 | } |
| 131 | |
| 132 | add_action( 'manage_posts_custom_column', 'give_render_form_columns', 10, 2 ); |
| 133 | |
| 134 | /** |
| 135 | * Registers the sortable columns in the list table |
| 136 | * |
| 137 | * @since 1.0 |
| 138 | * |
| 139 | * @param array $columns Array of the columns |
| 140 | * |
| 141 | * @return array $columns Array of sortable columns |
| 142 | */ |
| 143 | function give_sortable_form_columns( $columns ) { |
| 144 | $columns['price'] = 'amount'; |
| 145 | $columns['sales'] = 'sales'; |
| 146 | $columns['earnings'] = 'earnings'; |
| 147 | $columns['goal'] = 'goal'; |
| 148 | $columns['donations'] = 'donations'; |
| 149 | |
| 150 | return $columns; |
| 151 | } |
| 152 | |
| 153 | add_filter( 'manage_edit-give_forms_sortable_columns', 'give_sortable_form_columns' ); |
| 154 | |
| 155 | /** |
| 156 | * Sorts Columns in the Forms List Table |
| 157 | * |
| 158 | * @since 1.0 |
| 159 | * |
| 160 | * @param array $vars Array of all the sort variables. |
| 161 | * |
| 162 | * @return array $vars Array of all the sort variables. |
| 163 | */ |
| 164 | function give_sort_forms( $vars ) { |
| 165 | // Check if we're viewing the "give_forms" post type. |
| 166 | if ( ! isset( $vars['post_type'] ) || ! isset( $vars['orderby'] ) || 'give_forms' !== $vars['post_type'] ) { |
| 167 | return $vars; |
| 168 | } |
| 169 | |
| 170 | switch ( $vars['orderby'] ) { |
| 171 | // Check if 'orderby' is set to "sales". |
| 172 | case 'sales': |
| 173 | $vars = array_merge( |
| 174 | $vars, |
| 175 | array( |
| 176 | 'meta_key' => '_give_form_sales', |
| 177 | 'orderby' => 'meta_value_num', |
| 178 | ) |
| 179 | ); |
| 180 | break; |
| 181 | |
| 182 | // Check if "orderby" is set to "earnings". |
| 183 | case 'earnings': |
| 184 | $vars = array_merge( |
| 185 | $vars, |
| 186 | array( |
| 187 | 'meta_key' => '_give_form_earnings', |
| 188 | 'orderby' => 'meta_value_num', |
| 189 | ) |
| 190 | ); |
| 191 | break; |
| 192 | |
| 193 | // Check if "orderby" is set to "price/amount". |
| 194 | case 'amount': |
| 195 | $multi_level_meta_key = ( 'asc' === $vars['order'] ) ? '_give_levels_minimum_amount' : '_give_levels_maximum_amount'; |
| 196 | |
| 197 | $vars['orderby'] = 'meta_value_num'; |
| 198 | $vars['meta_query'] = array( |
| 199 | 'relation' => 'OR', |
| 200 | array( |
| 201 | 'key' => $multi_level_meta_key, |
| 202 | 'type' => 'NUMERIC', |
| 203 | ), |
| 204 | array( |
| 205 | 'key' => '_give_set_price', |
| 206 | 'type' => 'NUMERIC', |
| 207 | ) |
| 208 | ); |
| 209 | |
| 210 | break; |
| 211 | |
| 212 | // Check if "orderby" is set to "goal". |
| 213 | case 'goal': |
| 214 | $vars = array_merge( |
| 215 | $vars, |
| 216 | array( |
| 217 | 'meta_key' => '_give_set_goal', |
| 218 | 'orderby' => 'meta_value_num', |
| 219 | ) |
| 220 | ); |
| 221 | break; |
| 222 | |
| 223 | // Check if "orderby" is set to "donations". |
| 224 | case 'donations': |
| 225 | $vars = array_merge( |
| 226 | $vars, |
| 227 | array( |
| 228 | 'meta_key' => '_give_form_sales', |
| 229 | 'orderby' => 'meta_value_num', |
| 230 | ) |
| 231 | ); |
| 232 | break; |
| 233 | }// End switch(). |
| 234 | |
| 235 | return $vars; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Sets restrictions on author of Forms List Table |
| 240 | * |
| 241 | * @since 1.0 |
| 242 | * |
| 243 | * @param array $vars Array of all sort variables. |
| 244 | * |
| 245 | * @return array Array of all sort variables. |
| 246 | */ |
| 247 | function give_filter_forms( $vars ) { |
| 248 | if ( isset( $vars['post_type'] ) && 'give_forms' == $vars['post_type'] ) { |
| 249 | |
| 250 | // If an author ID was passed, use it |
| 251 | if ( isset( $_REQUEST['author'] ) && ! current_user_can( 'view_give_reports' ) ) { |
| 252 | |
| 253 | $author_id = $_REQUEST['author']; |
| 254 | if ( (int) $author_id !== get_current_user_id() ) { |
| 255 | wp_die( esc_html__( 'You do not have permission to view this data.', 'give' ), esc_html__( 'Error', 'give' ), array( |
| 256 | 'response' => 403, |
| 257 | ) ); |
| 258 | } |
| 259 | $vars = array_merge( |
| 260 | $vars, |
| 261 | array( |
| 262 | 'author' => get_current_user_id(), |
| 263 | ) |
| 264 | ); |
| 265 | |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return $vars; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Form Load |
| 274 | * |
| 275 | * Sorts the form columns. |
| 276 | * |
| 277 | * @since 1.0 |
| 278 | * @return void |
| 279 | */ |
| 280 | function give_forms_load() { |
| 281 | add_filter( 'request', 'give_sort_forms' ); |
| 282 | add_filter( 'request', 'give_filter_forms' ); |
| 283 | } |
| 284 | |
| 285 | add_action( 'load-edit.php', 'give_forms_load', 9999 ); |
| 286 | |
| 287 | /** |
| 288 | * Remove Forms Month Filter |
| 289 | * |
| 290 | * Removes the default drop down filter for forms by date. |
| 291 | * |
| 292 | * @since 1.0 |
| 293 | * |
| 294 | * @param array $dates The preset array of dates. |
| 295 | * |
| 296 | * @global $typenow The post type we are viewing. |
| 297 | * @return array Empty array disables the dropdown. |
| 298 | */ |
| 299 | function give_remove_month_filter( $dates ) { |
| 300 | global $typenow; |
| 301 | |
| 302 | if ( $typenow == 'give_forms' ) { |
| 303 | $dates = array(); |
| 304 | } |
| 305 | |
| 306 | return $dates; |
| 307 | } |
| 308 | |
| 309 | add_filter( 'months_dropdown_results', 'give_remove_month_filter', 99 ); |
| 310 | |
| 311 | /** |
| 312 | * Updates price when saving post |
| 313 | * |
| 314 | * @since 1.0 |
| 315 | * |
| 316 | * @param int $post_id Download (Post) ID |
| 317 | * |
| 318 | * @return int|null |
| 319 | */ |
| 320 | function give_price_save_quick_edit( $post_id ) { |
| 321 | if ( ! isset( $_POST['post_type'] ) || 'give_forms' !== $_POST['post_type'] ) { |
| 322 | return; |
| 323 | } |
| 324 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 325 | return $post_id; |
| 326 | } |
| 327 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 328 | return $post_id; |
| 329 | } |
| 330 | |
| 331 | if ( isset( $_REQUEST['_give_regprice'] ) ) { |
| 332 | give_update_meta( $post_id, '_give_set_price', give_sanitize_amount_for_db( strip_tags( stripslashes( $_REQUEST['_give_regprice'] ) ) ) ); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | add_action( 'save_post', 'give_price_save_quick_edit' ); |
| 337 | |
| 338 | /** |
| 339 | * Process bulk edit actions via AJAX |
| 340 | * |
| 341 | * @since 1.0 |
| 342 | * @return void |
| 343 | */ |
| 344 | function give_save_bulk_edit() { |
| 345 | |
| 346 | $post_ids = ( isset( $_POST['post_ids'] ) && ! empty( $_POST['post_ids'] ) ) ? $_POST['post_ids'] : array(); |
| 347 | |
| 348 | if ( ! empty( $post_ids ) && is_array( $post_ids ) ) { |
| 349 | $price = isset( $_POST['price'] ) ? strip_tags( stripslashes( $_POST['price'] ) ) : 0; |
| 350 | foreach ( $post_ids as $post_id ) { |
| 351 | |
| 352 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | if ( ! empty( $price ) ) { |
| 357 | give_update_meta( $post_id, '_give_set_price', give_sanitize_amount_for_db( $price ) ); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | die(); |
| 363 | } |
| 364 | |
| 365 | add_action( 'wp_ajax_give_save_bulk_edit', 'give_save_bulk_edit' ); |
| 366 |