Skip to content

Instantly share code, notes, and snippets.

@garvs
Created January 28, 2016 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garvs/93894e60e0c51d68b2f6 to your computer and use it in GitHub Desktop.
Save garvs/93894e60e0c51d68b2f6 to your computer and use it in GitHub Desktop.
Bulk update posts by ID list (category) to set User Role Editor Pro content view restrictions
<?php
/**
* Bulk update posts by ID list to set User Role Editor Pro content view restrictions
*
*/
add_action('admin_init', 'URE_Content_View_Restrictons_Bulk::update');
class URE_Content_View_Restrictons_Bulk {
const prohibit_allow_flag = 2; // Allow
const content_for_roles = 'abc'; // comma separated roles ID list
const post_access_error_action = 1; // HTTP 404 error
const post_access_error_message = ''; // error message
const categories = '55'; // comma separated list of categories
private static function get_posts_list() {
//$list = array(1260, 1261, 1262);
global $wpdb;
$query = 'SELECT object_id FROM '. $wpdb->prefix .'term_relationships where term_taxonomy_id in ('. self::categories .')';
$list = $wpdb->get_col($query);
return $list;
}
// end of get_posts_list()
private static function update_post_meta($post_id) {
update_post_meta($post_id, URE_Content_View_Restrictions::prohibit_allow_flag, self::prohibit_allow_flag);
update_post_meta($post_id, URE_Content_View_Restrictions::content_for_roles, self::content_for_roles);
update_post_meta($post_id, URE_Content_View_Restrictions::post_access_error_action, self::post_access_error_action);
update_post_meta($post_id, URE_Content_View_Restrictions::post_access_error_message, self::post_access_error_message);
}
// end of update_post_meta()
public static function update() {
if (!defined('URE_PLUGIN_DIR')) {
return;
}
require_once( URE_PLUGIN_DIR .'includes/pro/classes/content-view-restrictions.php');
$posts_list = self::get_posts_list();
$last_post_in_list = $posts_list[count($posts_list)-1];
$roles = get_post_meta($last_post_in_list, URE_Content_View_Restrictions::content_for_roles, true);
if ($roles==self::content_for_roles) { // already done - do not execute bulk update twice
return;
}
foreach($posts_list as $post_id) {
self::update_post_meta($post_id);
}
}
// end of ure_bulk_content_view_restrictions_update()
}
// end of URE_Content_View_Restrictons_Bulk class
@garvs
Copy link
Author

garvs commented Jan 28, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment