| 1 |
<?php |
| 2 |
|
| 3 |
class SimpleTags_Admin_Mass { |
| 4 |
|
| 5 |
const MENU_SLUG = 'st_options'; |
| 6 |
|
| 7 |
/** |
| 8 |
* SimpleTags_Admin_Mass constructor. |
| 9 |
*/ |
| 10 |
public function __construct() { |
| 11 |
// Ajax action, JS Helper and admin action |
| 12 |
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) ); |
| 13 |
|
| 14 |
// Admin menu |
| 15 |
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) ); |
| 16 |
|
| 17 |
// Register taxo, parent method... |
| 18 |
SimpleTags_Admin::register_taxonomy(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Add WP admin menu for Tags |
| 23 |
* |
| 24 |
* @return void |
| 25 |
* @author WebFactory Ltd |
| 26 |
*/ |
| 27 |
public static function admin_menu() { |
| 28 |
add_submenu_page( |
| 29 |
self::MENU_SLUG, |
| 30 |
__( 'TaxoPress: Mass Edit Terms', 'simple-tags' ), |
| 31 |
__( 'Mass Edit Terms', 'simple-tags' ), |
| 32 |
'simple_tags', |
| 33 |
'st_mass_terms', |
| 34 |
array( |
| 35 |
__CLASS__, |
| 36 |
'pageMassEditTags', |
| 37 |
) |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Control POST data for mass edit tags |
| 43 |
* |
| 44 |
*/ |
| 45 |
public static function admin_init() { |
| 46 |
if ( ! current_user_can( 'simple_tags' ) ) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
// Get GET data |
| 51 |
if ( isset( $_GET['post_type'] ) ) { |
| 52 |
$type = stripslashes( $_GET['post_type'] ); |
| 53 |
} |
| 54 |
|
| 55 |
if ( isset( $_POST['update_mass'] ) ) { |
| 56 |
// origination and intention |
| 57 |
if ( ! ( wp_verify_nonce( $_POST['secure_mass'], 'st_mass_terms' ) ) ) { |
| 58 |
add_settings_error( __CLASS__, __CLASS__, __( 'Security problem. Try again. If this problem persist, contact <a href="https://wordpress.org/support/plugin/simple-tags/#new-topic-0">plugin author</a>.', 'simple-tags' ), 'error' ); |
| 59 |
|
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
if ( isset( $_POST['tags'] ) ) { |
| 64 |
$counter = 0; |
| 65 |
foreach ( (array) $_POST['tags'] as $object_id => $tag_list ) { |
| 66 |
// Trim data |
| 67 |
$tag_list = trim( stripslashes( $tag_list ) ); |
| 68 |
|
| 69 |
// String to array |
| 70 |
$tags = explode( ',', $tag_list ); |
| 71 |
|
| 72 |
// Remove empty and trim tag |
| 73 |
$tags = array_filter( $tags, '_delete_empty_element' ); |
| 74 |
|
| 75 |
// Add new tag (no append ! replace !) |
| 76 |
wp_set_object_terms( $object_id, $tags, SimpleTags_Admin::$taxonomy ); |
| 77 |
$counter ++; |
| 78 |
|
| 79 |
// Clean cache |
| 80 |
clean_post_cache( $object_id ); |
| 81 |
} |
| 82 |
|
| 83 |
add_settings_error( __CLASS__, __CLASS__, sprintf( __( '%1$s %2$s(s) terms updated with success !', 'simple-tags' ), (int) $counter, strtolower( SimpleTags_Admin::$post_type_name ) ), 'updated' ); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* WP Page - Mass edit tags |
| 94 |
* |
| 95 |
*/ |
| 96 |
public static function pageMassEditTags() { |
| 97 |
global $wpdb, $wp_locale, $wp_query; |
| 98 |
list( $post_stati, $avail_post_stati ) = self::edit_data_query(); |
| 99 |
|
| 100 |
if ( ! isset( $_GET['paged'] ) ) { |
| 101 |
$_GET['paged'] = 1; |
| 102 |
} |
| 103 |
|
| 104 |
// Display message |
| 105 |
settings_errors( __CLASS__ ); |
| 106 |
?> |
| 107 |
<div class="wrap"> |
| 108 |
<?php SimpleTags_Admin::boxSelectorTaxonomy( 'st_mass_terms' ); ?> |
| 109 |
|
| 110 |
<form id="posts-filter" action="" method="get"> |
| 111 |
<input type="hidden" name="page" value="st_mass_terms"/> |
| 112 |
<input type="hidden" name="taxo" value="<?php echo esc_attr( SimpleTags_Admin::$taxonomy ); ?>"/> |
| 113 |
<input type="hidden" name="cpt" value="<?php echo esc_attr( SimpleTags_Admin::$post_type ); ?>"/> |
| 114 |
|
| 115 |
<h2><?php _e( 'Mass edit terms', 'simple-tags' ); ?></h2> |
| 116 |
<br> |
| 117 |
|
| 118 |
<ul class="subsubsub"> |
| 119 |
<?php |
| 120 |
$status_links = array(); |
| 121 |
$num_posts = wp_count_posts( SimpleTags_Admin::$post_type, 'readable' ); |
| 122 |
$class = ( empty( $_GET['post_status'] ) && empty( $_GET['post_type'] ) ) ? ' class="current"' : ''; |
| 123 |
$status_links[] = '<li><a href="' . admin_url( 'admin.php' ) . '?page=st_mass_terms&cpt=' . SimpleTags_Admin::$post_type . '&taxo=' . SimpleTags_Admin::$taxonomy . '"' . $class . '>' . __( 'All', 'simple-tags' ) . '</a>'; |
| 124 |
foreach ( $post_stati as $status => $label ) { |
| 125 |
$class = ''; |
| 126 |
|
| 127 |
if ( ! in_array( $status, $avail_post_stati ) ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
if ( empty( $num_posts->$status ) ) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
if ( isset( $_GET['post_status'] ) && $status == $_GET['post_status'] ) { |
| 135 |
$class = ' class="current"'; |
| 136 |
} |
| 137 |
|
| 138 |
$status_links[] = '<li><a href="' . admin_url( 'admin.php' ) . '?page=st_mass_terms&cpt=' . SimpleTags_Admin::$post_type . '&taxo=' . SimpleTags_Admin::$taxonomy . '&post_status=' . $status . '"' . $class . '>' . sprintf( _n( $label[2][0], $label[2][1], (int) $num_posts->$status ), number_format_i18n( $num_posts->$status ) ) . '</a>'; |
| 139 |
} |
| 140 |
echo implode( ' |</li>', $status_links ) . '</li>'; |
| 141 |
unset( $status_links ); |
| 142 |
|
| 143 |
$class = ( ! empty( $_GET['post_type'] ) ) ? ' class="current"' : ''; |
| 144 |
?> |
| 145 |
</ul> |
| 146 |
|
| 147 |
<?php if ( isset( $_GET['post_status'] ) ) : ?> |
| 148 |
<input type="hidden" name="post_status" value="<?php echo esc_attr( $_GET['post_status'] ) ?>"/> |
| 149 |
<?php endif; ?> |
| 150 |
|
| 151 |
<p class="search-box"> |
| 152 |
<input type="text" id="post-search-input" name="s" value="<?php the_search_query(); ?>"/> |
| 153 |
<input type="submit" value="<?php _e( 'Search', 'simple-tags' ); ?>" class="button"/> |
| 154 |
</p> |
| 155 |
|
| 156 |
<div class="tablenav"> |
| 157 |
<?php |
| 158 |
$posts_per_page = ( isset( $_GET['posts_per_page'] ) ) ? (int) $_GET['posts_per_page'] : 0; |
| 159 |
if ( (int) $posts_per_page == 0 ) { |
| 160 |
$posts_per_page = 15; |
| 161 |
} |
| 162 |
|
| 163 |
$page_links = paginate_links( array( |
| 164 |
'base' => add_query_arg( 'paged', '%#%' ), |
| 165 |
'format' => '', |
| 166 |
'total' => ceil( $wp_query->found_posts / $posts_per_page ), |
| 167 |
'current' => ( (int) $_GET['paged'] ) |
| 168 |
) ); |
| 169 |
|
| 170 |
if ( $page_links ) { |
| 171 |
echo "<div class='tablenav-pages'>$page_links</div>"; |
| 172 |
} |
| 173 |
?> |
| 174 |
|
| 175 |
<div style="float: left"> |
| 176 |
<?php |
| 177 |
if ( ! is_singular() ) { |
| 178 |
$arc_result = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = %s ORDER BY post_date DESC", SimpleTags_Admin::$post_type ) ); |
| 179 |
|
| 180 |
$month_count = count( $arc_result ); |
| 181 |
|
| 182 |
if ( ! isset( $_GET['m'] ) ) { |
| 183 |
$_GET['m'] = ''; |
| 184 |
} |
| 185 |
|
| 186 |
if ( $month_count && ! ( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { |
| 187 |
?> |
| 188 |
<select name='m'> |
| 189 |
<option<?php selected( @$_GET['m'], 0 ); ?> |
| 190 |
value='0'><?php _e( 'Show all dates', 'simple-tags' ); ?></option> |
| 191 |
<?php |
| 192 |
foreach ( $arc_result as $arc_row ) { |
| 193 |
if ( $arc_row->yyear == 0 ) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
$arc_row->mmonth = zeroise( $arc_row->mmonth, 2 ); |
| 197 |
|
| 198 |
if ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) { |
| 199 |
$default = ' selected="selected"'; |
| 200 |
} else { |
| 201 |
$default = ''; |
| 202 |
} |
| 203 |
|
| 204 |
echo "<option$default value='$arc_row->yyear$arc_row->mmonth'>"; |
| 205 |
echo $wp_locale->get_month( $arc_row->mmonth ) . " $arc_row->yyear"; |
| 206 |
echo "</option>\n"; |
| 207 |
} |
| 208 |
?> |
| 209 |
</select> |
| 210 |
<?php |
| 211 |
} |
| 212 |
?> |
| 213 |
|
| 214 |
<select name="posts_per_page" id="posts_per_page"> |
| 215 |
<option <?php if ( ! isset( $_GET['posts_per_page'] ) ) { |
| 216 |
echo 'selected="selected"'; |
| 217 |
} ?> value=""><?php _e( 'Quantity…', 'simple-tags' ); ?></option> |
| 218 |
<option <?php selected( $posts_per_page, 10 ); ?> value="10">10</option> |
| 219 |
<option <?php selected( $posts_per_page, 20 ); ?> value="20">20</option> |
| 220 |
<option <?php selected( $posts_per_page, 30 ); ?> value="30">30</option> |
| 221 |
<option <?php selected( $posts_per_page, 40 ); ?> value="40">40</option> |
| 222 |
<option <?php selected( $posts_per_page, 50 ); ?> value="50">50</option> |
| 223 |
<option <?php selected( $posts_per_page, 100 ); ?> value="100">100</option> |
| 224 |
<option <?php selected( $posts_per_page, 200 ); ?> value="200">200</option> |
| 225 |
</select> |
| 226 |
|
| 227 |
<input type="submit" id="post-query-submit" value="<?php _e( 'Filter', 'simple-tags' ); ?>" |
| 228 |
class="button-secondary"/> |
| 229 |
<?php } ?> |
| 230 |
</div> |
| 231 |
|
| 232 |
<br style="clear:both;"/> |
| 233 |
</div> |
| 234 |
</form> |
| 235 |
|
| 236 |
<br style="clear:both;"/> |
| 237 |
|
| 238 |
<?php if ( have_posts() ) : |
| 239 |
add_filter( 'the_title', 'esc_html' ); |
| 240 |
?> |
| 241 |
<form name="post" id="post" method="post" class="st-mass-edit"> |
| 242 |
<table class="widefat post fixed"> |
| 243 |
<thead> |
| 244 |
<tr> |
| 245 |
<th class="manage-column"><?php _e( 'Post title', 'simple-tags' ); ?></th> |
| 246 |
<th class="manage-column"><?php printf( __( 'Terms : %s', 'simple-tags' ), esc_html( SimpleTags_Admin::$taxo_name ) ); ?></th> |
| 247 |
</tr> |
| 248 |
</thead> |
| 249 |
<tbody> |
| 250 |
<?php |
| 251 |
$class = 'alternate'; |
| 252 |
while ( have_posts() ) { |
| 253 |
the_post(); |
| 254 |
$class = ( $class == 'alternate' ) ? '' : 'alternate'; |
| 255 |
?> |
| 256 |
<tr valign="top" class="<?php echo $class; ?>"> |
| 257 |
<th scope="row"><a |
| 258 |
href="<?php echo admin_url( 'post.php?action=edit&post=' . get_the_ID() ); ?>" |
| 259 |
title="<?php _e( 'Edit', 'simple-tags' ); ?>"><?php echo ( get_the_title() == '' ) ? the_ID() : the_title(); ?></a> |
| 260 |
</th> |
| 261 |
<td><input id="tags-input<?php the_ID(); ?>" class="autocomplete-input tags_input" |
| 262 |
type="text" size="100" name="tags[<?php the_ID(); ?>]" |
| 263 |
value="<?php echo SimpleTags_Admin::getTermsToEdit( SimpleTags_Admin::$taxonomy, get_the_ID() ); ?>"/> |
| 264 |
</td> |
| 265 |
</tr> |
| 266 |
<?php |
| 267 |
} |
| 268 |
?> |
| 269 |
</tbody> |
| 270 |
</table> |
| 271 |
|
| 272 |
<p class="submit"> |
| 273 |
<input type="hidden" name="secure_mass" |
| 274 |
value="<?php echo wp_create_nonce( 'st_mass_terms' ); ?>"/> |
| 275 |
<input class="button-primary" type="submit" name="update_mass" |
| 276 |
value="<?php _e( 'Update all »', 'simple-tags' ); ?>"/> |
| 277 |
</p> |
| 278 |
</form> |
| 279 |
|
| 280 |
<?php else: ?> |
| 281 |
|
| 282 |
<p><?php _e( 'No content to edit.', 'simple-tags' ); ?> |
| 283 |
|
| 284 |
<?php endif; ?> |
| 285 |
|
| 286 |
<?php SimpleTags_Admin::printAdminFooter(); ?> |
| 287 |
</div> |
| 288 |
<?php |
| 289 |
do_action( 'simpletags-mass_terms', SimpleTags_Admin::$taxonomy ); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Clone the core WP function, add the possibility to manage the post type |
| 294 |
* |
| 295 |
* @param bool $q |
| 296 |
* |
| 297 |
* @return array |
| 298 |
* @author WebFactory Ltd |
| 299 |
*/ |
| 300 |
public static function edit_data_query( $q = false ) { |
| 301 |
if ( false === $q ) { |
| 302 |
$q = $_GET; |
| 303 |
} |
| 304 |
|
| 305 |
// Date |
| 306 |
if ( isset( $q['m'] ) ) { |
| 307 |
$q['m'] = (int) $q['m']; |
| 308 |
} |
| 309 |
|
| 310 |
// Category |
| 311 |
if ( isset( $q['cat'] ) ) { |
| 312 |
$q['cat'] = (int) $q['cat']; |
| 313 |
} |
| 314 |
|
| 315 |
// Quantity |
| 316 |
$q['posts_per_page'] = ( isset( $q['posts_per_page'] ) ) ? (int) $q['posts_per_page'] : 0; |
| 317 |
if ( $q['posts_per_page'] == 0 ) { |
| 318 |
$q['posts_per_page'] = 15; |
| 319 |
} |
| 320 |
|
| 321 |
// Content type |
| 322 |
$q['post_type'] = SimpleTags_Admin::$post_type; |
| 323 |
|
| 324 |
// Post status |
| 325 |
$post_stati = array( // array( adj, noun ) |
| 326 |
'publish' => array( |
| 327 |
_x( 'Published', 'post' ), |
| 328 |
__( 'Published posts' ), |
| 329 |
_n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ) |
| 330 |
), |
| 331 |
'future' => array( |
| 332 |
_x( 'Scheduled', 'post' ), |
| 333 |
__( 'Scheduled posts' ), |
| 334 |
_n_noop( 'Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ) |
| 335 |
), |
| 336 |
'pending' => array( |
| 337 |
_x( 'Pending Review', 'post' ), |
| 338 |
__( 'Pending posts' ), |
| 339 |
_n_noop( 'Pending Review <span class="count">(%s)</span>', 'Pending Review <span class="count">(%s)</span>' ) |
| 340 |
), |
| 341 |
'draft' => array( |
| 342 |
_x( 'Draft', 'post' ), |
| 343 |
_x( 'Drafts', 'manage posts header' ), |
| 344 |
_n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ) |
| 345 |
), |
| 346 |
'private' => array( |
| 347 |
_x( 'Private', 'post' ), |
| 348 |
__( 'Private posts' ), |
| 349 |
_n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ) |
| 350 |
), |
| 351 |
'inherit' => array( |
| 352 |
_x( 'Inherit', 'post' ), |
| 353 |
__( 'Inherit posts' ), |
| 354 |
_n_noop( 'Inherit <span class="count">(%s)</span>', 'Inherit <span class="count">(%s)</span>' ) |
| 355 |
), |
| 356 |
); |
| 357 |
|
| 358 |
$post_stati = apply_filters( 'post_stati', $post_stati ); |
| 359 |
$avail_post_stati = get_available_post_statuses( SimpleTags_Admin::$post_type ); |
| 360 |
|
| 361 |
|
| 362 |
if($q['post_type'] === 'attachment'){ |
| 363 |
$q['post_status'] = 'inherit'; |
| 364 |
} |
| 365 |
|
| 366 |
$post_status_q = ''; |
| 367 |
if ( isset( $q['post_status'] ) && in_array( $q['post_status'], array_keys( $post_stati ) ) ) { |
| 368 |
$post_status_q = '&post_status=' . $q['post_status']; |
| 369 |
$post_status_q .= '&perm=readable'; |
| 370 |
} elseif ( ! isset( $q['post_status'] ) ) { |
| 371 |
$q['post_status'] = ''; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
if ( 'pending' === $q['post_status'] ) { |
| 376 |
$order = 'ASC'; |
| 377 |
$orderby = 'modified'; |
| 378 |
} elseif ( 'draft' === $q['post_status'] ) { |
| 379 |
$order = 'DESC'; |
| 380 |
$orderby = 'modified'; |
| 381 |
} else { |
| 382 |
$order = 'DESC'; |
| 383 |
$orderby = 'date'; |
| 384 |
} |
| 385 |
|
| 386 |
wp( "post_type={$q['post_type']}&what_to_show=posts$post_status_q&posts_per_page={$q['posts_per_page']}&order=$order&orderby=$orderby" ); |
| 387 |
|
| 388 |
return array( $post_stati, $avail_post_stati ); |
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
|