Example #1
0
 /**
  * Delete settings, data and metadata cache.
  * 
  * ## OPTIONS
  * 
  * <what>
  * : The type of data to be removed. Supported: all|settings|postdata|userdata|termdata|cache
  * 
  * [--assume-yes]
  * : Run in non interactive mode.
  * 
  * ## EXAMPLES
  * 
  *     wp amt clean all
  *     wp amt clean settings
  *     wp amt clean postdata
  *     wp amt clean userdata
  *     wp amt clean termdata
  *     wp amt clean cache
  *     wp amt clean cache --assume-yes
  *
  * @synopsis <all|settings|postdata|userdata|termdata|cache> [--assume-yes]
  */
 function clean($args, $assoc_args)
 {
     list($what) = $args;
     if (!in_array($what, array('all', 'settings', 'postdata', 'userdata', 'termdata', 'cache'))) {
         WP_CLI::error('Invalid argument: ' . $what . ' (valid: all|settings|postdata|userdata|termdata|cache)');
     }
     if ($assoc_args['assume-yes']) {
         WP_CLI::line(' ');
         WP_CLI::line('Running in non-interactive mode.');
         WP_CLI::line('Proceeding with ' . $what . ' cleanup...');
     } else {
         // Confirmation
         WP_CLI::line(' ');
         WP_CLI::line('This commands deletes Add-Meta-Tags data from the database.');
         WP_CLI::line('This action is final and cannot be undone.');
         WP_CLI::line(' ');
         echo 'Are you sure you want to do this?  Type \'yes\' to continue: ';
         $handle = fopen('php://stdin', 'r');
         $choice = fgets($handle);
         fclose($handle);
         if (trim($choice) != 'yes') {
             WP_CLI::line('Aborting...');
             exit;
         }
         WP_CLI::line(' ');
         WP_CLI::line('Proceeding with ' . $what . ' cleanup...');
     }
     // Delete AMT settings
     if ($what == 'settings' || $what == 'all') {
         delete_option('add_meta_tags_opts');
         WP_CLI::line('Deleted settings.');
     } elseif ($what == 'postdata' || $what == 'all') {
         $qr_args = array('numberposts' => -1, 'post_type' => 'any', 'post_status' => 'any', 'orderby' => 'id', 'order' => 'ASC', 'suppress_filters' => true);
         $posts_arr = get_posts($qr_args);
         $amt_post_fields = amt_get_post_custom_field_names();
         foreach ($posts_arr as $post) {
             foreach ($amt_post_fields as $amt_post_field) {
                 delete_post_meta($post->ID, $amt_post_field);
             }
         }
         WP_CLI::line('Deleted post custom fields.');
     } elseif ($what == 'userdata' || $what == 'all') {
         $qr_args = array('orderby' => 'login', 'order' => 'ASC', 'fields' => 'all');
         $users_arr = get_users($qr_args);
         $amt_user_fields = amt_get_user_custom_field_names();
         foreach ($users_arr as $user) {
             foreach ($amt_user_fields as $amt_user_field) {
                 delete_user_meta($user->ID, $amt_user_field);
             }
         }
         WP_CLI::line('Deleted user meta fields.');
     } elseif ($what == 'termdata' || $what == 'all') {
         // Get taxonomies
         // Get the custom taxonomy names.
         // Arguments in order to retrieve all public custom taxonomies
         $tax_args = array('public' => true, '_builtin' => true);
         $tax_output = 'names';
         // names or objects
         $tax_operator = 'and';
         // 'and' or 'or'
         $taxonomies = get_taxonomies($tax_args, $tax_output, $tax_operator);
         // Get terms
         $qr_args = array('orderby' => 'id', 'order' => 'ASC', 'fields' => 'all');
         $terms_arr = get_terms($taxonomies, $qr_args);
         // Iterate over our fields and export
         $amt_term_fields = amt_get_term_custom_field_names();
         foreach ($terms_arr as $term) {
             foreach ($amt_term_fields as $amt_term_field) {
                 delete_term_meta($term->term_id, $amt_term_field);
             }
         }
         WP_CLI::line('Deleted term meta fields.');
     } elseif ($what == 'cache' || $what == 'all') {
         // Transients may not be cached in the database, but in a different storage backend.
         // So, here amt_delete_all_transient_metadata_cache() is not used.
         //$result = amt_delete_all_transient_metadata_cache();
         //WP_CLI::line( sprintf('Deleted %d transient metadata cache entries.', $result) );
         global $wpdb;
         // Get the current blog id.
         $blog_id = get_current_blog_id();
         // Construct the options table name for the current blog
         $posts_table = $wpdb->get_blog_prefix($blog_id) . 'posts';
         //var_dump($posts_table);
         // Construct SQL query that fetched the post IDs.
         $sql = "SELECT ID FROM {$posts_table} WHERE post_status = 'publish'";
         // Get number of cache entries
         $results = $wpdb->get_results($sql);
         foreach ($results as $post) {
             // Delete the metadata cache for this post object
             amt_delete_transient_cache_for_post(absint($post->ID));
         }
         WP_CLI::line(sprintf('Force purged cached metadata of %d published post objects.', count($results)));
     }
     WP_CLI::success('Data clean up complete.');
 }
Example #2
0
function amt_purge_transient_cache_post_comments($comment_id)
{
    $comment = get_comment($comment_id);
    $post_id = $comment->comment_post_ID;
    if (absint($post_id) <= 0) {
        return;
    }
    // Get the options the DB
    $options = get_option("add_meta_tags_opts");
    if (absint($options['transient_cache_expiration']) > 0) {
        // Purge transient data
        amt_delete_transient_cache_for_post($post_id);
    }
}