/wp-content/plugins/download-monitor/src/Admin/CustomActions.php

https://github.com/livinglab/openlab · PHP · 307 lines · 169 code · 54 blank · 84 comment · 48 complexity · e447443eaf58b02eea244c3738328982 MD5 · raw file

  1. <?php
  2. class DLM_Custom_Actions {
  3. /**
  4. * Setup custom actions
  5. */
  6. public function setup() {
  7. add_filter( 'request', array( $this, 'sort_columns' ) );
  8. add_action( "restrict_manage_posts", array( $this, "downloads_by_category" ) );
  9. add_action( 'delete_post', array( $this, 'delete_post' ) );
  10. // bulk and quick edit
  11. add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit' ), 10, 2 );
  12. add_action( 'quick_edit_custom_box', array( $this, 'quick_edit' ), 10, 2 );
  13. add_action( 'save_post', array( $this, 'bulk_and_quick_edit_save_post' ), 10, 2 );
  14. }
  15. /**
  16. * downloads_by_category function.
  17. *
  18. * @access public
  19. *
  20. * @param int $show_counts (default: 1)
  21. * @param int $hierarchical (default: 1)
  22. * @param int $show_uncategorized (default: 1)
  23. * @param string $orderby (default: '')
  24. *
  25. * @return void
  26. */
  27. public function downloads_by_category( $show_counts = 1, $hierarchical = 1, $show_uncategorized = 1, $orderby = '' ) {
  28. global $typenow, $wp_query;
  29. if ( $typenow != 'dlm_download' ) {
  30. return;
  31. }
  32. $r = array();
  33. $r['pad_counts'] = 1;
  34. $r['hierarchical'] = $hierarchical;
  35. $r['hide_empty'] = 1;
  36. $r['show_count'] = $show_counts;
  37. $r['selected'] = ( isset( $wp_query->query['dlm_download_category'] ) ) ? $wp_query->query['dlm_download_category'] : '';
  38. $r['menu_order'] = false;
  39. if ( $orderby == 'order' ) {
  40. $r['menu_order'] = 'asc';
  41. } elseif ( $orderby ) {
  42. $r['orderby'] = $orderby;
  43. }
  44. $terms = get_terms( 'dlm_download_category', $r );
  45. if ( ! $terms ) {
  46. return;
  47. }
  48. $dlm_download_category = isset( $_GET['dlm_download_category'] ) ? sanitize_text_field( wp_unslash( $_GET['dlm_download_category'] ) ) : '';
  49. echo "<select name='dlm_download_category' id='dropdown_dlm_download_category'>";
  50. echo '<option value="" ' . selected( $dlm_download_category, '', false ) . '>' . esc_html__( 'Select a category', 'download-monitor' ) . '</option>';
  51. echo $this->walk_category_dropdown_tree( $terms, 0, $r ); //phpcs:ignore
  52. echo '</select>';
  53. }
  54. /**
  55. * Walk the Product Categories.
  56. *
  57. * @access public
  58. * @return string
  59. */
  60. private function walk_category_dropdown_tree() {
  61. $args = func_get_args();
  62. // the user's options are the third parameter
  63. if ( empty( $args[2]['walker'] ) || ! is_a( $args[2]['walker'], 'Walker' ) ) {
  64. $walker = new DLM_Category_Walker();
  65. } else {
  66. $walker = $args[2]['walker'];
  67. }
  68. return call_user_func_array( array( $walker, 'walk' ), $args );
  69. }
  70. /**
  71. * delete_post function.
  72. *
  73. * @access public
  74. *
  75. * @param int $id
  76. *
  77. * @return void
  78. */
  79. public function delete_post( $id ) {
  80. global $wpdb;
  81. if ( ! current_user_can( 'delete_posts' ) ) {
  82. return;
  83. }
  84. if ( $id > 0 ) {
  85. $post_type = get_post_type( $id );
  86. switch ( $post_type ) {
  87. case 'dlm_download' :
  88. $versions = get_children( 'post_parent=' . $id . '&post_type=dlm_download_version' );
  89. if ( is_array( $versions ) && count( $versions ) > 0 ) {
  90. foreach ( $versions as $child ) {
  91. wp_delete_post( $child->ID, true );
  92. }
  93. }
  94. break;
  95. }
  96. }
  97. }
  98. /**
  99. * sort_columns function.
  100. *
  101. * @access public
  102. *
  103. * @param array $vars
  104. *
  105. * @return array
  106. */
  107. public function sort_columns( $vars ) {
  108. if ( isset( $vars['orderby'] ) ) {
  109. if ( 'download_id' == $vars['orderby'] ) {
  110. $vars['orderby'] = 'ID';
  111. } elseif ( 'download_count' == $vars['orderby'] ) {
  112. $vars = array_merge( $vars, array(
  113. 'meta_key' => '_download_count',
  114. 'orderby' => 'meta_value_num'
  115. ) );
  116. } elseif ( 'featured' == $vars['orderby'] ) {
  117. $vars = array_merge( $vars, array(
  118. 'meta_key' => '_featured',
  119. 'orderby' => 'meta_value'
  120. ) );
  121. } elseif ( 'members_only' == $vars['orderby'] ) {
  122. $vars = array_merge( $vars, array(
  123. 'meta_key' => '_members_only',
  124. 'orderby' => 'meta_value'
  125. ) );
  126. } elseif ( 'redirect_only' == $vars['orderby'] ) {
  127. $vars = array_merge( $vars, array(
  128. 'meta_key' => '_redirect_only',
  129. 'orderby' => 'meta_value'
  130. ) );
  131. }
  132. }
  133. return $vars;
  134. }
  135. /**
  136. * Custom bulk edit - form
  137. *
  138. * @param mixed $column_name
  139. * @param mixed $post_type
  140. */
  141. public function quick_edit( $column_name, $post_type ) {
  142. // only on our PT
  143. if ( 'dlm_download' != $post_type || 'featured' != $column_name ) {
  144. return;
  145. }
  146. // nonce field
  147. wp_nonce_field( 'dlm_quick_edit_nonce', 'dlm_quick_edit_nonce' );
  148. $this->bulk_quick_edit_fields();
  149. }
  150. /**
  151. * Custom bulk edit - form
  152. *
  153. * @param mixed $column_name
  154. * @param mixed $post_type
  155. */
  156. public function bulk_edit( $column_name, $post_type ) {
  157. // only on our PT
  158. if ( 'dlm_download' != $post_type || 'featured' != $column_name ) {
  159. return;
  160. }
  161. // nonce field
  162. wp_nonce_field( 'dlm_bulk_edit_nonce', 'dlm_bulk_edit_nonce' );
  163. $this->bulk_quick_edit_fields();
  164. }
  165. /**
  166. * Output the build and quick edit fields
  167. */
  168. private function bulk_quick_edit_fields() {
  169. ?>
  170. <fieldset class="inline-edit-col-right inline-edit-col-dlm">
  171. <div class="inline-edit-col inline-edit-col-dlm-inner">
  172. <span class="title"><?php echo esc_html__( 'Download Monitor Data', 'download-monitor' ); ?></span><br/>
  173. <label for="_featured"><input type="checkbox" name="_featured" id="_featured" value="1"/><?php echo esc_html__( 'Featured download', 'download-monitor' ); ?></label>
  174. <label for="_members_only"><input type="checkbox" name="_members_only" id="_members_only" value="1"/><?php echo esc_html__( 'Members only', 'download-monitor' ); ?></label>
  175. <label for="_redirect_only"><input type="checkbox" name="_redirect_only" id="_redirect_only" value="1"/><?php echo esc_html__( 'Redirect to file', 'download-monitor' ); ?></label>
  176. </div>
  177. </fieldset>
  178. <?php
  179. }
  180. /**
  181. * Quick and bulk edit saving
  182. *
  183. * @param int $post_id
  184. * @param WP_Post $post
  185. *
  186. * @return int
  187. */
  188. public function bulk_and_quick_edit_save_post( $post_id, $post ) {
  189. // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  190. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  191. return $post_id;
  192. }
  193. // Don't save revisions and autosaves
  194. if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
  195. return $post_id;
  196. }
  197. // Check post type is product
  198. if ( 'dlm_download' != $post->post_type ) {
  199. return $post_id;
  200. }
  201. // Check user permission
  202. if ( ! current_user_can( 'manage_downloads', $post_id ) ) {
  203. return $post_id;
  204. }
  205. // handle bulk
  206. if ( isset( $_REQUEST['dlm_bulk_edit_nonce'] ) ) {
  207. // check nonce
  208. // phpcs:ignore
  209. if ( ! wp_verify_nonce( $_REQUEST['dlm_bulk_edit_nonce'], 'dlm_bulk_edit_nonce' ) ) {
  210. return $post_id;
  211. }
  212. // set featured
  213. if ( isset( $_REQUEST['_featured'] ) ) {
  214. update_post_meta( $post_id, '_featured', 'yes' );
  215. }
  216. // set members only
  217. if ( isset( $_REQUEST['_members_only'] ) ) {
  218. update_post_meta( $post_id, '_members_only', 'yes' );
  219. }
  220. // set redirect only
  221. if ( isset( $_REQUEST['_redirect_only'] ) ) {
  222. update_post_meta( $post_id, '_redirect_only', 'yes' );
  223. }
  224. }
  225. // handle quick
  226. if ( isset( $_REQUEST['dlm_quick_edit_nonce'] ) ) {
  227. // check nonce
  228. // phpcs:ignore
  229. if ( ! wp_verify_nonce( $_REQUEST['dlm_quick_edit_nonce'], 'dlm_quick_edit_nonce' ) ) {
  230. return $post_id;
  231. }
  232. // set featured
  233. if ( isset( $_REQUEST['_featured'] ) ) {
  234. update_post_meta( $post_id, '_featured', 'yes' );
  235. } else {
  236. update_post_meta( $post_id, '_featured', 'no' );
  237. }
  238. // set members only
  239. if ( isset( $_REQUEST['_members_only'] ) ) {
  240. update_post_meta( $post_id, '_members_only', 'yes' );
  241. } else {
  242. update_post_meta( $post_id, '_members_only', 'no' );
  243. }
  244. // set redirect only
  245. if ( isset( $_REQUEST['_redirect_only'] ) ) {
  246. update_post_meta( $post_id, '_redirect_only', 'yes' );
  247. } else {
  248. update_post_meta( $post_id, '_redirect_only', 'no' );
  249. }
  250. }
  251. return $post_id;
  252. }
  253. }