PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/data/wpcom-themes/guruq/guruq-list.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 407 lines | 333 code | 70 blank | 4 comment | 104 complexity | 379bf1e31c2ad7118bb6de9e85932c38 MD5 | raw file
  1. <?php
  2. global $wpdb, $wp_locale;
  3. if ( !current_user_can('edit_posts') )
  4. wp_die(__('Cheatin&#8217; uh?'));
  5. $edit_link = add_query_arg( array( 'page' => GURUQ_SLUG ), 'edit.php' );
  6. // Back-compat for viewing comments of an entry
  7. if ( $_redirect = intval( max( @$_GET['p'], @$_GET['attachment_id'], @$_GET['page_id'] ) ) ) {
  8. wp_redirect( admin_url('edit-comments.php?p=' . $_redirect ) );
  9. exit;
  10. } else {
  11. unset( $_redirect );
  12. }
  13. // Handle bulk actions
  14. if ( isset($_GET['doaction']) || isset($_GET['doaction2']) || isset($_GET['delete_all']) || isset($_GET['delete_all2']) || isset($_GET['bulk_edit']) ) {
  15. check_admin_referer('bulk-posts');
  16. $sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
  17. if ( strpos($sendback, 'post.php') !== false )
  18. $sendback = admin_url('post-new.php');
  19. if ( isset($_GET['delete_all']) || isset($_GET['delete_all2']) ) {
  20. $post_status = preg_replace('/[^a-z0-9_-]+/i', '', $_GET['post_status']);
  21. $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='post' AND post_status = %s", $post_status ) );
  22. $doaction = 'delete';
  23. } elseif ( ( $_GET['action'] != -1 || $_GET['action2'] != -1 ) && ( isset($_GET['post']) || isset($_GET['ids']) ) ) {
  24. $post_ids = isset($_GET['post']) ? array_map( 'intval', (array) $_GET['post'] ) : explode(',', $_GET['ids']);
  25. $doaction = ($_GET['action'] != -1) ? $_GET['action'] : $_GET['action2'];
  26. } else {
  27. wp_redirect( admin_url( $edit_link ) );
  28. }
  29. switch ( $doaction ) {
  30. case 'trash':
  31. $trashed = 0;
  32. foreach( (array) $post_ids as $post_id ) {
  33. if ( !current_user_can('delete_post', $post_id) )
  34. wp_die( __('You are not allowed to move this post to the trash.') );
  35. if ( !wp_trash_post($post_id) )
  36. wp_die( __('Error in moving to trash...') );
  37. $trashed++;
  38. }
  39. $sendback = add_query_arg( array('trashed' => $trashed, 'ids' => join(',', $post_ids)), $sendback );
  40. break;
  41. case 'untrash':
  42. $untrashed = 0;
  43. foreach( (array) $post_ids as $post_id ) {
  44. if ( !current_user_can('delete_post', $post_id) )
  45. wp_die( __('You are not allowed to restore this post from the trash.') );
  46. if ( !wp_untrash_post($post_id) )
  47. wp_die( __('Error in restoring from trash...') );
  48. $untrashed++;
  49. }
  50. $sendback = add_query_arg('untrashed', $untrashed, $sendback);
  51. break;
  52. case 'delete':
  53. $deleted = 0;
  54. foreach( (array) $post_ids as $post_id ) {
  55. $post_del = & get_post($post_id);
  56. if ( !current_user_can('delete_post', $post_id) )
  57. wp_die( __('You are not allowed to delete this post.') );
  58. if ( $post_del->post_type == 'attachment' ) {
  59. if ( ! wp_delete_attachment($post_id) )
  60. wp_die( __('Error in deleting...') );
  61. } else {
  62. if ( !wp_delete_post($post_id) )
  63. wp_die( __('Error in deleting...') );
  64. }
  65. $deleted++;
  66. }
  67. $sendback = add_query_arg('deleted', $deleted, $sendback);
  68. break;
  69. case 'edit':
  70. $done = bulk_edit_posts($_GET);
  71. if ( is_array($done) ) {
  72. $done['updated'] = count( $done['updated'] );
  73. $done['skipped'] = count( $done['skipped'] );
  74. $done['locked'] = count( $done['locked'] );
  75. $sendback = add_query_arg( $done, $sendback );
  76. }
  77. break;
  78. }
  79. if ( isset($_GET['action']) )
  80. $sendback = remove_query_arg( array('action', 'action2', 'cat', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view', 'post_type'), $sendback );
  81. wp_redirect($sendback);
  82. exit();
  83. } elseif ( isset($_GET['_wp_http_referer']) && ! empty($_GET['_wp_http_referer']) ) {
  84. wp_redirect( remove_query_arg( array('_wp_http_referer', '_wpnonce'), stripslashes($_SERVER['REQUEST_URI']) ) );
  85. exit;
  86. }
  87. if ( empty($title) )
  88. $title = __( GURUQ_CAT );
  89. $parent_file = 'edit.php';
  90. $user_posts = false;
  91. if ( !current_user_can('edit_others_posts') ) {
  92. $user_posts_count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(1) FROM $wpdb->posts WHERE post_type = 'post' AND post_status != 'trash' AND post_author = %d", $current_user->ID) );
  93. $user_posts = true;
  94. if ( $user_posts_count && empty($_GET['post_status']) && empty($_GET['all_posts']) && empty($_GET['author']) )
  95. $_GET['author'] = $current_user->ID;
  96. }
  97. list($post_stati, $avail_post_stati) = wp_edit_posts_query();
  98. if ( !isset( $_GET['paged'] ) )
  99. $_GET['paged'] = 1;
  100. if ( empty($_GET['mode']) )
  101. $mode = 'list';
  102. else
  103. $mode = esc_attr($_GET['mode']); ?>
  104. <div class="wrap">
  105. <?php screen_icon(); ?>
  106. <h2><?php echo esc_html( $title ); ?>
  107. <?php
  108. if ( isset($_GET['s']) && $_GET['s'] )
  109. printf( '<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', esc_html( get_search_query() ) ); ?>
  110. </h2>
  111. <?php
  112. if ( isset($_GET['posted']) && $_GET['posted'] ) : $_GET['posted'] = (int) $_GET['posted']; ?>
  113. <div id="message" class="updated fade"><p><strong><?php _e('Your post has been saved.'); ?></strong> <a href="<?php echo get_permalink( $_GET['posted'] ); ?>"><?php _e('View post'); ?></a> | <a href="<?php echo get_edit_post_link( $_GET['posted'] ); ?>"><?php _e('Edit post'); ?></a></p></div>
  114. <?php $_SERVER['REQUEST_URI'] = remove_query_arg(array('posted'), $_SERVER['REQUEST_URI']);
  115. endif; ?>
  116. <?php if ( isset($_GET['locked']) || isset($_GET['skipped']) || isset($_GET['updated']) || isset($_GET['deleted']) || isset($_GET['trashed']) || isset($_GET['untrashed']) ) { ?>
  117. <div id="message" class="updated fade"><p>
  118. <?php if ( isset($_GET['updated']) && (int) $_GET['updated'] ) {
  119. printf( _n( '%s post updated.', '%s posts updated.', $_GET['updated'] ), number_format_i18n( $_GET['updated'] ) );
  120. unset($_GET['updated']);
  121. }
  122. if ( isset($_GET['skipped']) && (int) $_GET['skipped'] )
  123. unset($_GET['skipped']);
  124. if ( isset($_GET['locked']) && (int) $_GET['locked'] ) {
  125. printf( _n( '%s post not updated, somebody is editing it.', '%s posts not updated, somebody is editing them.', $_GET['locked'] ), number_format_i18n( $_GET['locked'] ) );
  126. unset($_GET['locked']);
  127. }
  128. if ( isset($_GET['deleted']) && (int) $_GET['deleted'] ) {
  129. printf( _n( 'Post permanently deleted.', '%s posts permanently deleted.', $_GET['deleted'] ), number_format_i18n( $_GET['deleted'] ) );
  130. unset($_GET['deleted']);
  131. }
  132. if ( isset($_GET['trashed']) && (int) $_GET['trashed'] ) {
  133. printf( _n( 'Post moved to the trash.', '%s posts moved to the trash.', $_GET['trashed'] ), number_format_i18n( $_GET['trashed'] ) );
  134. $ids = isset($_GET['ids']) ? $_GET['ids'] : 0;
  135. echo ' <a href="' . esc_url( wp_nonce_url( "edit.php?doaction=undo&action=untrash&ids=$ids", "bulk-posts" ) ) . '">' . __('Undo?') . '</a><br />';
  136. unset($_GET['trashed']);
  137. }
  138. if ( isset($_GET['untrashed']) && (int) $_GET['untrashed'] ) {
  139. printf( _n( 'Post restored from the trash.', '%s posts restored from the trash.', $_GET['untrashed'] ), number_format_i18n( $_GET['untrashed'] ) );
  140. unset($_GET['undeleted']);
  141. }
  142. $_SERVER['REQUEST_URI'] = remove_query_arg( array('locked', 'skipped', 'updated', 'deleted', 'trashed', 'untrashed'), $_SERVER['REQUEST_URI'] );
  143. ?>
  144. </p></div>
  145. <?php
  146. }
  147. $guruq_cat_id = (int) category_exists( GURUQ_CAT );
  148. $cat = get_category_by_slug( GURUQ_CAT );
  149. $total = (int) $cat->category_count;
  150. $pagenum = 1;
  151. $per_page = 20;
  152. $num_pages = (int) ( $total / $per_page );
  153. // set array of args for get_posts
  154. $post_args = array();
  155. if ( isset( $_GET['pagenum'] ) )
  156. $post_args['pagenum'] = intval( $_GET['pagenum'] ) > 0 ? intval( $_GET['pagenum'] ) : 1;
  157. if ( isset( $_GET['s'] ) )
  158. $post_args['s'] = $_GET['s'];
  159. if ( isset( $_GET['post_status'] ) ) {
  160. $post_args['post_status'] = $_GET['post_status'];
  161. } else {
  162. // Filter out post_status
  163. add_filter( 'pre_get_posts', 'guruq_remove_post_status' );
  164. }
  165. $post_args['numberposts'] = $per_page;
  166. $post_args['category'] = $guruq_cat_id;
  167. $post_args['post_type'] = 'post';
  168. $posts = get_posts( $post_args );
  169. ?>
  170. <form id="posts-filter" action="<?php echo $edit_link; ?>" method="get">
  171. <ul class="subsubsub">
  172. <?php
  173. if ( empty($locked_post_status) ) :
  174. $status_links = array();
  175. $num_posts = guruq_count_posts( 'post', 'readable', GURUQ_CAT );
  176. $class = '';
  177. $allposts = '';
  178. if ( 0 < $guruq_cat_id ) {
  179. $wp_query->query_vars['cat'] = $guruq_cat_id;
  180. }
  181. if ( $user_posts ) {
  182. if ( $_GET['author'] == $current_user->ID )
  183. $class = ' class="current"';
  184. $status_links[] = "<li><a href='" . add_query_arg( array( 'author' => $current_user->ID ), $edit_link ) . "'$class>" . sprintf( _nx( 'My Posts <span class="count">(%s)</span>', 'My Posts <span class="count">(%s)</span>', $user_posts_count, 'posts' ), number_format_i18n( $user_posts_count ) ) . '</a>';
  185. $allposts = '?all_posts=1';
  186. }
  187. $total_posts = array_sum( (array) $num_posts ) - $num_posts->trash;
  188. $class = empty($class) && empty($_GET['post_status']) ? ' class="current"' : '';
  189. $status_links[] = "<li><a href='$edit_link{$allposts}'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts' ), number_format_i18n( $total_posts ) ) . '</a>';
  190. foreach ( $post_stati as $status => $label ) {
  191. $class = '';
  192. if ( !in_array( $status, $avail_post_stati ) )
  193. continue;
  194. if ( empty( $num_posts->$status ) )
  195. continue;
  196. if ( isset($_GET['post_status']) && $status == $_GET['post_status'] )
  197. $class = ' class="current"';
  198. $edit_link = add_query_arg( array( 'post_status' => $status ), $edit_link );
  199. $status_links[] = "<li><a href='$edit_link'$class>" . sprintf( _n( $label[2][0], $label[2][1], $num_posts->$status ), number_format_i18n( $num_posts->$status ) ) . '</a>';
  200. }
  201. echo implode( " |</li>\n", $status_links ) . '</li>';
  202. unset( $status_links );
  203. endif;
  204. ?>
  205. </ul>
  206. <p class="search-box">
  207. <input type="hidden" name="page" value="<?php echo GURUQ_SLUG; ?>" />
  208. <label class="screen-reader-text" for="post-search-input"><?php _e( 'Search Posts' ); ?>:</label>
  209. <input type="text" id="post-search-input" name="s" value="<?php the_search_query(); ?>" />
  210. <input type="submit" value="<?php esc_attr_e( 'Search Posts' ); ?>" class="button" />
  211. </p>
  212. <input type="hidden" name="post_status" class="post_status_page" value="<?php echo !empty($_GET['post_status']) ? esc_attr($_GET['post_status']) : 'all'; ?>" />
  213. <input type="hidden" name="mode" value="<?php echo esc_attr($mode); ?>" />
  214. <?php if ( have_posts() ) { ?>
  215. <div class="tablenav">
  216. <?php
  217. $page_links = paginate_links( array(
  218. 'base' => add_query_arg( 'paged', '%#%' ),
  219. 'format' => '',
  220. 'prev_text' => __('&laquo;'),
  221. 'next_text' => __('&raquo;'),
  222. 'total' => $wp_query->max_num_pages,
  223. 'current' => $_GET['paged']
  224. ));
  225. $is_trash = isset($_GET['post_status']) && $_GET['post_status'] == 'trash';
  226. ?>
  227. <div class="alignleft actions">
  228. <select name="action">
  229. <option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
  230. <?php if ( $is_trash ) { ?>
  231. <option value="untrash"><?php _e('Restore'); ?></option>
  232. <option value="delete"><?php _e('Delete Permanently'); ?></option>
  233. <?php } else { ?>
  234. <option value="edit"><?php _e('Edit'); ?></option>
  235. <option value="trash"><?php _e('Move to Trash'); ?></option>
  236. <?php } ?>
  237. </select>
  238. <input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction" id="doaction" class="button-secondary action" />
  239. <?php wp_nonce_field('bulk-posts'); ?>
  240. <?php // view filters
  241. if ( !is_singular() ) {
  242. $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'post' ORDER BY post_date DESC";
  243. $arc_result = $wpdb->get_results( $arc_query );
  244. $month_count = count($arc_result);
  245. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) {
  246. $m = isset($_GET['m']) ? (int)$_GET['m'] : 0;
  247. ?>
  248. <select name='m'>
  249. <option<?php selected( $m, 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
  250. <?php
  251. foreach ($arc_result as $arc_row) {
  252. if ( $arc_row->yyear == 0 )
  253. continue;
  254. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  255. if ( $arc_row->yyear . $arc_row->mmonth == $m )
  256. $default = ' selected="selected"';
  257. else
  258. $default = '';
  259. echo "<option$default value='" . esc_attr("$arc_row->yyear$arc_row->mmonth") . "'>";
  260. echo $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear";
  261. echo "</option>\n";
  262. }
  263. ?>
  264. </select>
  265. <?php } ?>
  266. <?php
  267. do_action('restrict_manage_posts');
  268. ?>
  269. <input type="submit" id="post-query-submit" value="<?php esc_attr_e('Filter'); ?>" class="button-secondary" />
  270. <?php }
  271. if ( $is_trash && current_user_can('edit_others_posts') ) { ?>
  272. <input type="submit" name="delete_all" id="delete_all" value="<?php esc_attr_e('Empty Trash'); ?>" class="button-secondary apply" />
  273. <?php } ?>
  274. </div>
  275. <?php if ( $page_links ) { ?>
  276. <div class="tablenav-pages"><?php $page_links_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s&#8211;%s of %s' ) . '</span>%s',
  277. number_format_i18n( ( $_GET['paged'] - 1 ) * $wp_query->query_vars['posts_per_page'] + 1 ),
  278. number_format_i18n( min( $_GET['paged'] * $wp_query->query_vars['posts_per_page'], $wp_query->found_posts ) ),
  279. number_format_i18n( $wp_query->found_posts ),
  280. $page_links
  281. ); echo $page_links_text; ?></div>
  282. <?php } ?>
  283. <div class="clear"></div>
  284. </div>
  285. <div class="clear"></div>
  286. <table class="widefat post fixed" cellspacing="0">
  287. <thead>
  288. <tr>
  289. <?php print_column_headers('edit'); ?>
  290. </tr>
  291. </thead>
  292. <tfoot>
  293. <tr>
  294. <?php print_column_headers('edit', false); ?>
  295. </tr>
  296. </tfoot>
  297. <tbody>
  298. <?php post_rows( $posts ); ?>
  299. </tbody>
  300. </table>
  301. <div class="tablenav">
  302. <?php
  303. if ( $page_links )
  304. echo "<div class='tablenav-pages'>$page_links_text</div>";
  305. ?>
  306. <div class="alignleft actions">
  307. <select name="action2">
  308. <option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
  309. <?php if ( $is_trash ) { ?>
  310. <option value="untrash"><?php _e('Restore'); ?></option>
  311. <option value="delete"><?php _e('Delete Permanently'); ?></option>
  312. <?php } else { ?>
  313. <option value="edit"><?php _e('Edit'); ?></option>
  314. <option value="trash"><?php _e('Move to Trash'); ?></option>
  315. <?php } ?>
  316. </select>
  317. <input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction2" id="doaction2" class="button-secondary action" />
  318. <?php if ( $is_trash && current_user_can('edit_others_posts') ) { ?>
  319. <input type="submit" name="delete_all2" id="delete_all2" value="<?php esc_attr_e('Empty Trash'); ?>" class="button-secondary apply" />
  320. <?php } ?>
  321. <br class="clear" />
  322. </div>
  323. <br class="clear" />
  324. </div>
  325. <?php } else { // have_posts() ?>
  326. <div class="clear"></div>
  327. <p><?php
  328. if ( 'trash' == $_GET['post_status'] )
  329. _e('No posts found in the trash');
  330. else
  331. _e('No posts found');
  332. ?></p>
  333. <?php } ?>
  334. </form>
  335. <div id="ajax-response"></div>
  336. <br class="clear" />
  337. </div>