PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/core-addons/coupons/basic-coupons/admin.php

https://github.com/ArzuA/gitwordpress
PHP | 537 lines | 372 code | 52 blank | 113 comment | 69 complexity | 5aebd3c11274446b71b84c9a0c3874ba MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Functions / hooks only needed in the admin
  4. * @package IT_Exchange
  5. * @since 0.4.0
  6. */
  7. /**
  8. * Enqueues CSS / JS on add / edit page
  9. *
  10. * @since 0.4.0
  11. *
  12. * @return void
  13. */
  14. function it_exchange_basic_coupons_enqueue_js_css() {
  15. $screen = get_current_screen();
  16. $current_filter = current_filter();
  17. // Abort if screen wasn't found
  18. if ( empty( $screen ) )
  19. return;
  20. // Abort if not adding, editing or on the coupons list screen.
  21. if ( 'exchange_page_it-exchange-edit-basic-coupon' == $screen->base || 'exchange_page_it-exchange-add-basic-coupon' == $screen->base || 'edit-it_exchange_coupon' == $screen->id ) {
  22. // Enqueue JS / CSS based on current filter
  23. if ( 'admin_print_scripts' == $current_filter ) {
  24. // JS
  25. $deps = array( 'jquery', 'jquery-ui-tooltip', 'jquery-ui-datepicker' );
  26. wp_enqueue_script( 'it-exchange-add-edit-coupon', ITUtility::get_url_from_file( dirname( __FILE__ ) ) . '/js/add-edit-coupon.js', $deps );
  27. } else if ( 'admin_print_styles' == $current_filter ) {
  28. // CSS
  29. $deps = array( 'jquery-ui-tooltip', 'jquery-ui-datepicker' );
  30. wp_enqueue_style( 'it-exchange-add-edit-coupon', ITUtility::get_url_from_file( dirname( __FILE__ ) ) . '/css/add-edit-coupon.css' );
  31. }
  32. }
  33. }
  34. add_action( 'admin_print_styles', 'it_exchange_basic_coupons_enqueue_js_css' );
  35. add_action( 'admin_print_scripts', 'it_exchange_basic_coupons_enqueue_js_css' );
  36. /**
  37. * Adds Basic Coupons post type to list of post type to remove the quick edit
  38. *
  39. * @since 0.4.5
  40. *
  41. * @return array list of post types
  42. */
  43. function it_exchange_remove_quick_edit_from_basic_coupons( $post_types ) {
  44. $post_types[] = 'it_exchange_coupon';
  45. return $post_types;
  46. }
  47. add_filter( 'it_exchange_remove_quick_edit_from_post_types', 'it_exchange_remove_quick_edit_from_basic_coupons', 10 );
  48. /**
  49. * Saves a coupon
  50. *
  51. * @since 0.4.0
  52. *
  53. * @return void
  54. */
  55. function it_exchange_basic_coupons_save_coupon() {
  56. if ( empty( $_POST['it-exchange-basic-coupons-add-edit-coupon'] ) )
  57. return;
  58. // Redirect to All coupons if cancel button was submited
  59. if ( ! empty( $_POST['cancel'] ) ) {
  60. wp_safe_redirect( add_query_arg( array( 'post_type' => 'it_exchange_coupon' ), 'edit.php' ) );
  61. die();
  62. }
  63. $nonce = empty( $_POST['_wpnonce'] ) ? false : $_POST['_wpnonce'];
  64. if ( ! wp_verify_nonce( $nonce, 'it-exchange-basic-coupons-add-edit-coupon' ) )
  65. return;
  66. $data = ITForm::get_post_data();
  67. if ( ! it_exchange_basic_coupons_data_is_valid() )
  68. return;
  69. // Remove hidden field
  70. unset( $data['add-edit-coupon'] );
  71. // Convert name to post_title
  72. $data['post_title'] = $data['name'];
  73. unset( $data['name'] );
  74. // Update message or added message
  75. $msg = empty( $data['ID'] ) ? 'added' : 'updated';
  76. // Convert code, amount-number, amount-type, start-date, end-date to meta
  77. $data['post_meta']['_it-basic-code'] = $data['code'];
  78. $data['post_meta']['_it-basic-amount-number'] = it_exchange_convert_to_database_number( $data['amount-number'] );
  79. $data['post_meta']['_it-basic-amount-type'] = $data['amount-type'];
  80. $data['post_meta']['_it-basic-start-date'] = $data['start-date'];
  81. $data['post_meta']['_it-basic-end-date'] = $data['end-date'];
  82. $data['post_meta']['_it-basic-limit-quantity'] = $data['limit-quantity'];
  83. $data['post_meta']['_it-basic-quantity'] = $data['quantity'];
  84. $data['post_meta']['_it-basic-limit-product'] = $data['limit-product'];
  85. $data['post_meta']['_it-basic-product-id'] = $data['product-id'];
  86. $data['post_meta']['_it-basic-limit-frequency'] = $data['limit-frequency'];
  87. $data['post_meta']['_it-basic-frequency-times'] = $data['frequency-times'];
  88. $data['post_meta']['_it-basic-frequency-length'] = $data['frequency-length'];
  89. $data['post_meta']['_it-basic-frequency-units'] = $data['frequency-units'];
  90. unset( $data['code'] );
  91. unset( $data['amount-number'] );
  92. unset( $data['amount-type'] );
  93. unset( $data['start-date'] );
  94. unset( $data['end-date'] );
  95. unset( $data['limit-quantity'] );
  96. unset( $data['quantity'] );
  97. unset( $data['limit-product'] );
  98. unset( $data['product-id'] );
  99. unset( $data['limit-frequency'] );
  100. unset( $data['frequency-times'] );
  101. unset( $data['frequency-length'] );
  102. unset( $data['frequency-units'] );
  103. if ( $post_id = it_exchange_add_coupon( $data ) ) {
  104. wp_safe_redirect( add_query_arg( array( 'post_type' => 'it_exchange_coupon' ), get_admin_url() . 'edit.php' ) );
  105. }
  106. }
  107. add_action( 'admin_init', 'it_exchange_basic_coupons_save_coupon' );
  108. /**
  109. * Vaidates coupon data
  110. *
  111. * @since 0.4.0
  112. *
  113. * @return boolean
  114. */
  115. function it_exchange_basic_coupons_data_is_valid() {
  116. $data = ITForm::get_post_data();
  117. if ( empty( $data['name'] ) )
  118. it_exchange_add_message( 'error', __( 'Coupon Name cannot be left empty', 'it-l10n-ithemes-exchange' ) );
  119. if ( empty( $data['code'] ) )
  120. it_exchange_add_message( 'error', __( 'Coupon Code cannot be left empty', 'it-l10n-ithemes-exchange' ) );
  121. if ( empty( $data['amount-number'] ) )
  122. it_exchange_add_message( 'error', __( 'Coupon Discount cannot be left empty', 'it-l10n-ithemes-exchange' ) );
  123. if ( ! is_numeric( $data['amount-number'] ) || trim( $data['amount-number'] ) < 1 )
  124. it_exchange_add_message( 'error', __( 'Coupon Discount must be a postive number', 'it-l10n-ithemes-exchange' ) );
  125. if ( ! empty( $data['limit-quantity'] ) && ! is_numeric( $data['quantity'] ) )
  126. it_exchange_add_message( 'error', __( 'Available Coupons must be a number', 'it-l10n-ithemes-exchange' ) );
  127. if ( ! empty( $data['limit-product'] ) && ! it_exchange_get_product( $data['product-id'] ) )
  128. it_exchange_add_message( 'error', __( 'Please select a product.', 'it-l10n-ithemes-exchange' ) );
  129. if ( ! empty( $data['limit-frequency'] ) && ! is_numeric( $data['frequency-times'] ) && ! is_numeric( $data['frequency-length'] ) )
  130. it_exchange_add_message( 'error', __( 'Please select a frequency limitation', 'it-l10n-ithemes-exchange' ) );
  131. return ! it_exchange_has_messages( 'error' );
  132. }
  133. /**
  134. * This adds a menu item to the Exchange menu pointing to the WP All [post_type] table
  135. *
  136. * @since 0.4.0
  137. *
  138. * @return void
  139. */
  140. function it_exchange_basic_coupons_add_menu_item() {
  141. if ( ! empty( $_GET['page'] ) && 'it-exchange-add-basic-coupon' == $_GET['page'] ) {
  142. $slug = 'it-exchange-add-basic-coupon';
  143. $func = 'it_exchange_basic_coupons_print_add_edit_coupon_screen';
  144. add_submenu_page( 'it-exchange', __( 'Add Coupon', 'it-l10n-ithemes-exchange' ), __( 'Add Coupon', 'it-l10n-ithemes-exchange' ), 'update_plugins', $slug, $func );
  145. } else if ( ! empty( $_GET['page'] ) && 'it-exchange-edit-basic-coupon' == $_GET['page'] ) {
  146. $slug = 'it-exchange-edit-basic-coupon';
  147. $func = 'it_exchange_basic_coupons_print_add_edit_coupon_screen';
  148. add_submenu_page( 'it-exchange', __( 'Edit Coupon', 'it-l10n-ithemes-exchange' ), __( 'Edit Coupon', 'it-l10n-ithemes-exchange' ), 'update_plugins', $slug, $func );
  149. }
  150. $url = add_query_arg( array( 'post_type' => 'it_exchange_coupon' ), 'edit.php' );
  151. add_submenu_page( 'it-exchange', __( 'Coupons', 'it-l10n-ithemes-exchange' ), __( 'Coupons', 'it-l10n-ithemes-exchange' ), 'update_plugins', $url );
  152. }
  153. add_action( 'admin_menu', 'it_exchange_basic_coupons_add_menu_item' );
  154. /**
  155. * Redirects admin users away from core add / edit post type screens for coupons to our custom ones.
  156. *
  157. * @since 0.4.0
  158. *
  159. * @return void
  160. */
  161. function it_exchange_basic_coupons_redirect_core_add_edit_screens() {
  162. $pagenow = empty( $GLOBALS['pagenow'] ) ? false : $GLOBALS['pagenow'];
  163. $post_type = empty( $_GET['post_type'] ) ? false : $_GET['post_type'];
  164. $post_id = empty( $_GET['post'] ) ? false : $_GET['post'];
  165. $action = empty( $_GET['action'] ) ? false : $_GET['action'];
  166. if ( ! $pagenow || ( 'post-new.php' != $pagenow && 'post.php' != $pagenow ) )
  167. return;
  168. // Redirect for add new screen
  169. if ( 'post-new.php' == $pagenow && 'it_exchange_coupon' == $post_type ) {
  170. wp_safe_redirect( add_query_arg( array( 'page' => 'it-exchange-add-basic-coupon' ), get_admin_url() . 'admin.php' ) );
  171. die();
  172. }
  173. // Redirect for edit screen
  174. if ( in_array( $action, array( 'delete', 'trash', 'untrash' ) ) )
  175. return;
  176. $coupon = new IT_Exchange_Coupon( $post_id );
  177. if ( 'post.php' == $pagenow ) {
  178. if ( $post_id && 'it_exchange_coupon' == $coupon->post_type ) {
  179. wp_safe_redirect( add_query_arg( array( 'page' => 'it-exchange-edit-basic-coupon', 'post' => $post_id ), get_admin_url() . 'admin.php' ) );
  180. die();
  181. }
  182. }
  183. }
  184. add_action( 'admin_init', 'it_exchange_basic_coupons_redirect_core_add_edit_screens' );
  185. /**
  186. * Prints the add coupon screen
  187. *
  188. * @since 0.4.0
  189. *
  190. * @return void;
  191. */
  192. function it_exchange_basic_coupons_print_add_edit_coupon_screen() {
  193. // Setup add / edit variables
  194. $post_id = empty( $_GET['post'] ) ? false : $_GET['post'];
  195. $heading = $post_id ? __( 'Edit Coupon', 'it-l10n-ithemes-exchange' ) : __( 'Add Coupon', 'it-l10n-ithemes-exchange' );
  196. $form_action = $post_id ? add_query_arg( array( 'page' => 'it-exchange-edit-basic-coupon', 'post' => $post_id ), get_admin_url() . 'admin.php' ) : add_query_arg( array( 'page' => 'it-exchange-add-basic-coupon' ), get_admin_url() . 'admin.php' );
  197. // Set form values
  198. if ( $post_id ) {
  199. $coupon = new IT_Exchange_Coupon( $post_id );
  200. $amount = it_exchange_convert_from_database_number( $coupon->amount_number );
  201. if ( 'amount' == $coupon->amount_type )
  202. $amount = it_exchange_format_price( $amount, false );
  203. $values['name'] = $coupon->post_title;
  204. $values['code'] = $coupon->code;
  205. $values['amount-number'] = $amount;
  206. $values['amount-type'] = $coupon->amount_type;
  207. $values['start-date'] = $coupon->start_date;
  208. $values['end-date'] = $coupon->end_date;
  209. $values['limit-quantity'] = $coupon->limit_quantity;
  210. $values['quantity'] = $coupon->quantity;
  211. $values['limit-product'] = $coupon->limit_product;
  212. $values['product-id'] = $coupon->product_id;
  213. $values['limit-frequency'] = $coupon->limit_frequency;
  214. $values['frequency-times'] = $coupon->frequency_times;
  215. $values['frequency-length'] = $coupon->frequency_length;
  216. $values['frequency-units'] = $coupon->frequency_units;
  217. }
  218. $errors = it_exchange_get_messages( 'error' );
  219. if ( ! empty( $errors ) ) {
  220. foreach( $errors as $error ) {
  221. ITUtility::show_error_message( $error );
  222. }
  223. } else if ( ! empty( $_GET['added'] ) ) {
  224. ITUtility::show_status_message( __( 'Coupon Added', 'it-l10n-ithemes-exchange' ) );
  225. } else if ( ! empty( $_GET['updated'] ) ) {
  226. ITUtility::show_status_message( __( 'Coupon Updated', 'it-l10n-ithemes-exchange' ) );
  227. }
  228. $form_values = empty( $values ) ? ITForm::get_post_data() : $values;
  229. $form_values = ! empty( $errors ) ? ITForm::get_post_data() : $form_values;
  230. $form = new ITForm( $form_values, array( 'prefix' => 'it-exchange-basic-coupons' ) );
  231. $form_options = array(
  232. 'id' => apply_filters( 'it-exchange-basic-coupons_form_id', 'it-exchange-basic-coupons' ),
  233. 'enctype' => apply_filters( 'it-exchange-basic-coupons_enctype', false ),
  234. 'action' => $form_action,
  235. );
  236. ?>
  237. <div class="wrap">
  238. <?php
  239. ITUtility::screen_icon( 'it-exchange-coupons' );
  240. echo '<h2>' . $heading . '</h2>';
  241. $form->start_form( $form_options, 'it-exchange-basic-coupons-add-edit-coupon' );
  242. if ( $post_id )
  243. $form->add_hidden( 'ID', $post_id );
  244. $form->add_hidden( 'add-edit-coupon', true );
  245. ?>
  246. <div class="it-exchange-add-basic-coupon">
  247. <div class="fields">
  248. <div class="field">
  249. <label for="name"><?php _e( 'Name', 'it-l10n-ithemes-exchange' ); ?> <span class="tip" title="<?php _e( 'What do you want to call this coupon? This is just for your reference.', 'it-l10n-ithemes-exchange' ); ?>">i</span></label>
  250. <?php $form->add_text_box( 'name' ); ?>
  251. </div>
  252. <div class="field coupon-code">
  253. <label for="code"><?php _e( 'Code', 'it-l10n-ithemes-exchange' ); ?> <span class="tip" title="<?php _e( 'Try something cool like EXCHANGERULEZ5000! Or click the dice to generate a random code.', 'it-l10n-ithemes-exchange' ); ?>">i</span></label>
  254. <?php $form->add_text_box( 'code', array( 'class' => 'emptycode' ) ); ?>
  255. <a href class="dice" title="Generate a random code."><img src="<?php echo esc_attr( ITUtility::get_url_from_file( dirname( __FILE__ ) ) ); ?>/images/dice-t.png" /></a>
  256. </div>
  257. <div class="field amount">
  258. <label for="amount-number"><?php _e( 'Amount', 'it-l10n-ithemes-exchange' ); ?></label>
  259. <?php $form->add_text_box( 'amount-number', array( 'type' => 'number' ) ); ?>
  260. <?php
  261. $settings = it_exchange_get_option( 'settings_general' );
  262. $currency = $settings['default-currency'];
  263. $symbol = it_exchange_get_currency_symbol( $currency );
  264. ?>
  265. <?php $form->add_drop_down( 'amount-type', array( '%' => __( '% Percent', 'it-l10n-ithemes-exchange' ), 'amount' => $symbol . ' ' . $currency ) ); ?>
  266. </div>
  267. <div class="field date" data-alert="<?php _e( 'Please select an end date that is after the start date.', 'it-l10n-ithemes-exchange' ); ?>">
  268. <div class="start-date">
  269. <label for="start-date"><?php _e( 'Start Date', 'it-l10n-ithemes-exchange' ); ?></label>
  270. <?php $form->add_text_box( 'start-date', array( 'class' => 'datepicker', 'data-append' => 'end-date' ) ); ?>
  271. </div>
  272. <div class="end-date">
  273. <label for="end-date"><?php _e( 'End Date', 'it-l10n-ithemes-exchange' ); ?></label>
  274. <?php $form->add_text_box( 'end-date', array( 'class' => 'datepicker', 'data-append' => 'start-date' ) ); ?>
  275. </div>
  276. </div>
  277. <div class="field limit-quantity">
  278. <?php $form->add_check_box( 'limit-quantity' ); ?>
  279. <label for="limit-quantity">
  280. <?php _e( 'Limit number of coupons', 'it-l10n-ithemes-exchange' ); ?>
  281. <span class="tip" title="<?php esc_attr_e( __( 'Check to limit the number of times this coupon can be used', 'it-l10n-ithemes-exchange' ) ); ?>">i</span>
  282. </label>
  283. </div>
  284. <div class="field quantity">
  285. <?php $form->add_text_box( 'quantity', array( 'type' => 'number' ) ); ?>
  286. <span class="tip" title="<?php _e( 'How many times can this coupon be used before it is disabled?', 'it-l10n-ithemes-exchange' ); ?>">i</span>
  287. </div>
  288. <div class="field limit-product">
  289. <?php $form->add_check_box( 'limit-product' ); ?>
  290. <label for="limit-product">
  291. <?php _e( 'Limit to a specific product', 'it-l10n-ithemes-exchange' ); ?>
  292. <span class="tip" title="<?php esc_attr_e( __( 'Check to limit the coupon discount to a specific product price, not the cart total', 'it-l10n-ithemes-exchange' ) ); ?>">i</span>
  293. </label>
  294. </div>
  295. <div class="field product-id">
  296. <?php
  297. $product_options = array( 0 => __( 'Select a product', 'it-l10n-ithemes-exchange' ) );
  298. $products = it_exchange_get_products( array( 'show_hidden' => true, 'posts_per_page' => -1 ) );
  299. foreach( (array) $products as $id => $product ) {
  300. $product_options[$product->ID] = $product->post_title;
  301. }
  302. ?>
  303. <?php $form->add_drop_down( 'product-id', $product_options ); ?>
  304. <span class="tip" title="<?php _e( 'Select a product to use with this coupon.', 'it-l10n-ithemes-exchange' ); ?>">i</span>
  305. </div>
  306. <div class="field limit-frequency">
  307. <?php $form->add_check_box( 'limit-frequency' ); ?>
  308. <label for="limit-frequency">
  309. <?php _e( 'Limit frequency of use per customer', 'it-l10n-ithemes-exchange' ); ?>
  310. <span class="tip" title="<?php esc_attr_e( __( 'Check to limit the number of times each customer can use the coupon during a specified time frame', 'it-l10n-ithemes-exchange' ) ); ?>">i</span>
  311. </label>
  312. </div>
  313. <div class="field frequency-limitations">
  314. <?php
  315. $thirty = array();
  316. for( $i=1;$i<=30;$i++ ) {
  317. $thirty[$i] = $i;
  318. }
  319. $frequency_times = apply_filters( 'it_exchange_limit_coupon_freqency_times_options', $thirty );
  320. $frequency_length = apply_filters( 'it_exchange_limit_coupon_freqency_length_options', $thirty );
  321. $frequency_units = array( 'day' => __( 'Day(s)', 'it-l10n-ithemes-exchange' ), 'week' => __( 'Week(s)', 'it-l10n-ithemes-exchange' ), 'year' => __( 'Year(s)', 'it-l10n-ithemes-exchange' ) );
  322. _e( 'Limit this coupon to ', 'it-l10n-ithemes-exchange' );
  323. $form->add_drop_down( 'frequency-times', $frequency_times );
  324. _e( ' use(s) per customer for every ', 'it-l10n-ithemes-exchange' );
  325. $form->add_drop_down( 'frequency-length', $frequency_length );
  326. $form->add_drop_down( 'frequency-units', $frequency_units );
  327. ?>
  328. </div>
  329. <div class="field">
  330. <?php $form->add_submit( 'cancel', array( 'class' => 'button-large button', 'value' => __( 'Cancel', 'it-l10n-ithemes-exchange' ) ) ); ?>
  331. <?php $form->add_submit( 'submit', array( 'class' => 'button-large button-primary button', 'value' => __( 'Save', 'it-l10n-ithemes-exchange' ) ) ); ?>
  332. </div>
  333. </div>
  334. </div>
  335. <?php
  336. $form->end_form();
  337. ?>
  338. </div>
  339. <?php
  340. }
  341. /**
  342. * Remove Custom add coupon and edit coupon links from submenu
  343. *
  344. * @since 0.4.0
  345. *
  346. * @return void
  347. */
  348. function it_exchange_basic_coupons_remove_submenu_links() {
  349. if ( ! empty( $GLOBALS['submenu']['it-exchange'] ) ) {
  350. foreach( $GLOBALS['submenu']['it-exchange'] as $key => $sub ) {
  351. if ( 'it-exchange-add-basic-coupon' == $sub[2] || 'it-exchange-edit-basic-coupon' == $sub[2] ) {
  352. // Remove the extra coupons submenu item
  353. unset( $GLOBALS['submenu']['it-exchange'][$key] );
  354. // Mark the primary coupons submenu item as current
  355. $GLOBALS['submenu_file'] = 'edit.php?post_type=it_exchange_coupon';
  356. }
  357. }
  358. }
  359. }
  360. add_action( 'admin_head', 'it_exchange_basic_coupons_remove_submenu_links' );
  361. /**
  362. * Adds the coupon specific columns to the View All Coupons table
  363. *
  364. * @since 0.4.0
  365. * @param array $existing exisiting columns array
  366. * @return array modified columns array
  367. */
  368. function it_exchange_basic_coupons_product_columns( $existing ) {
  369. $columns['cb'] = '<input type="checkbox" />';
  370. $columns['title'] = __( 'Title', 'it-l10n-ithemes-exchange' );
  371. $columns['it_exchange_coupon_code'] = __( 'Coupon Code', 'it-l10n-ithemes-exchange' );
  372. $columns['it_exchange_coupon_discount'] = __( 'Discount', 'it-l10n-ithemes-exchange' );
  373. $columns['it_exchange_coupon_start_date'] = __( 'Start Date', 'it-l10n-ithemes-exchange' );
  374. $columns['it_exchange_coupon_end_date'] = __( 'End Date', 'it-l10n-ithemes-exchange' );
  375. $columns['it_exchange_coupon_quantity'] = __( 'Available Coupons', 'it-l10n-ithemes-exchange' );
  376. $columns['it_exchange_coupon_product_id'] = __( 'Product', 'it-l10n-ithemes-exchange' );
  377. return $columns;
  378. }
  379. add_filter( 'manage_edit-it_exchange_coupon_columns', 'it_exchange_basic_coupons_product_columns', 999 );
  380. /**
  381. * Makes the custom columns added above sortable
  382. *
  383. * @since 0.4.0
  384. * @param array $sortables existing sortable columns
  385. * @return array modified sortable columnns
  386. */
  387. function it_exchange_basic_coupons_sortable_columns( $sortables ) {
  388. $sortables['it_exchange_coupon_code'] = 'it-exchange-coupon-code';
  389. $sortables['it_exchange_coupon_discount'] = 'it-exchange-coupon-discount';
  390. $sortables['it_exchange_coupon_start_date'] = 'it-exchange-coupon-start-date';
  391. $sortables['it_exchange_coupon_end_date'] = 'it-exchange-coupon-end-date';
  392. $sortables['it_exchange_coupon_quantity'] = 'it-exchange-coupon-quantity';
  393. $sortables['it_exchange_coupon_product_id'] = 'it-exchange-coupon-product-id';
  394. return $sortables;
  395. }
  396. add_filter( 'manage_edit-it_exchange_coupon_sortable_columns', 'it_exchange_basic_coupons_sortable_columns' );
  397. /**
  398. * Adds the data to the custom columns
  399. *
  400. * @since 0.4.0
  401. * @param string $column column title
  402. * @return void
  403. */
  404. function it_exchange_basic_coupons_custom_column_info( $column ) {
  405. global $post;
  406. $coupon = it_exchange_get_coupon( $post );
  407. switch( $column ) {
  408. case 'it_exchange_coupon_code':
  409. esc_attr_e( $coupon->code );
  410. break;
  411. case 'it_exchange_coupon_discount':
  412. echo esc_attr( it_exchange_get_coupon_discount_label( $coupon ) );
  413. break;
  414. case 'it_exchange_coupon_start_date':
  415. esc_attr_e( $coupon->start_date );
  416. break;
  417. case 'it_exchange_coupon_end_date':
  418. esc_attr_e( $coupon->end_date );
  419. break;
  420. case 'it_exchange_coupon_quantity':
  421. $quantity_label = ( empty( $coupon->limit_quantity ) ) ? __( 'Unlimited', 'it-l10n-ithemes-exchange' ) : $coupon->quantity;
  422. esc_attr_e( $quantity_label );
  423. break;
  424. case 'it_exchange_coupon_product_id':
  425. $product_name = ( empty( $coupon->limit_product ) ) ? __( 'All Products', 'it-l10n-ithemes-exchange' ) : it_exchange_get_product_feature( $coupon->product_id, 'title' );
  426. esc_attr_e( $product_name );
  427. break;
  428. }
  429. }
  430. add_filter( 'manage_it_exchange_coupon_posts_custom_column', 'it_exchange_basic_coupons_custom_column_info' );
  431. /**
  432. * Modify sort of coupons in edit.php for custom columns
  433. *
  434. * @since 0.4.0F
  435. *
  436. * @param string $request original request
  437. */
  438. function it_exchange_basic_coupons_modify_wp_query_request_on_edit_php( $request ) {
  439. global $hook_suffix;
  440. if ( 'edit.php' === $hook_suffix ) {
  441. if ( 'it_exchange_coupon' === $request['post_type'] && isset( $request['orderby'] ) ) {
  442. switch( $request['orderby'] ) {
  443. case 'it-exchange-coupon-code' :
  444. $request['orderby'] = 'meta_value';
  445. $request['meta_key'] = '_it-basic-code';
  446. break;
  447. case 'it-exchange-coupon-discount':
  448. $request['orderby'] = 'meta_value_num';
  449. $request['meta_key'] = '_it-basic-amount-number';
  450. break;
  451. case 'it-exchange-coupon-start-date':
  452. $request['orderby'] = 'meta_value_date';
  453. $request['meta_key'] = '_it-basic-start-date';
  454. break;
  455. case 'it-exchange-coupon-end-date':
  456. $request['orderby'] = 'meta_value_date';
  457. $request['meta_key'] = '_it-basic-end-date';
  458. break;
  459. case 'it-exchange-coupon-quantity':
  460. $request['orderby'] = 'meta_value_num';
  461. $request['meta_key'] = '_it-basic-quantity';
  462. break;
  463. case 'it-exchange-coupon-product-id':
  464. $request['orderby'] = 'meta_value_num';
  465. $request['meta_key'] = '_it-basic-product-id';
  466. break;
  467. }
  468. }
  469. }
  470. return $request;
  471. }
  472. add_filter( 'request', 'it_exchange_basic_coupons_modify_wp_query_request_on_edit_php' );
  473. /**
  474. * Register our pages as an exchange pages so that exchange CSS class is applied to admin body
  475. *
  476. * @since 0.4.17
  477. *
  478. * @param array $pages existing pages
  479. * @return array
  480. */
  481. function it_exchange_basic_coupons_register_exchange_admin_page( $pages ) {
  482. $pages[] = 'it-exchange-add-basic-coupon';
  483. return $pages;
  484. }
  485. add_filter( 'it_exchange_admin_pages', 'it_exchange_basic_coupons_register_exchange_admin_page' );