Skip to content

Instantly share code, notes, and snippets.

@eighty20results
Last active May 31, 2016 15:17
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 eighty20results/a762c940b1d7ce3c2ab8876efbc907ef to your computer and use it in GitHub Desktop.
Save eighty20results/a762c940b1d7ce3c2ab8876efbc907ef to your computer and use it in GitHub Desktop.
Proof of Concept for bulk-updating posts with membership protection (if the post didn't previously have protection)
<?php
/*
Plugin Name: PMPro Bulk Update
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Allow updating all posts to a membership level
Version: .1
Author: Thomas Sjolshage <thomas@eighty20results.com>
Author URI: http://www.eighty20results.com/thomas-sjolshagen/
*/
function pmprobu_update_all_posts() {
error_log("Trigger update of posts to set membership level(s)");
global $wpdb;
$nSQL = $wpdb->prepare("
SELECT p.ID
FROM {$wpdb->posts} AS p
WHERE ((post_type = 'post') AND (p.ID NOT IN (SELECT page_id FROM {$wpdb->pmpro_memberships_pages})))
");
$not_protected = $wpdb->get_col($nSQL);
if (!empty( $not_protected)) {
$sql = array();
$sql[] = "
INSERT INTO {$wpdb->pmpro_memberships_pages} ( membership_id, modified, page_id )
SELECT 1, NOW(), p.ID
FROM {$wpdb->posts} AS p
WHERE (p.post_type = 'post') AND (p.ID NOT IN (" . implode(",", $not_protected) . "))";
$sql[] = "
INSERT INTO {$wpdb->pmpro_memberships_pages} ( membership_id, modified, page_id )
SELECT 2, NOW(), p.ID
FROM {$wpdb->posts} AS p
WHERE (p.post_type = 'post') AND (p.ID NOT IN (" . implode(",", $not_protected) . "))";
$sql[] = "
INSERT INTO {$wpdb->pmpro_memberships_pages} ( membership_id, modified, page_id )
SELECT 3, NOW(), p.ID
FROM {$wpdb->posts} AS p
WHERE (p.post_type = 'post') AND (p.ID NOT IN (" . implode(",", $not_protected) . "))";
foreach( $sql as $s ) {
error_log("Wanting to run query: {$s}");
if (false === $wpdb->query($s)) {
wp_die("Failure while updating the page/protection information! Error: {$wpdb->last_error}" );
} else {
error_log("Successfully updated Database w/new post protection");
}
}
}
}
register_activation_hook( __FILE__, 'pmprobu_update_all_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment