Changeset 3004128
- Timestamp:
- 12/01/2023 09:38:41 AM (2 years ago)
- Location:
- year-updater/trunk
- Files:
-
- 5 edited
-
includes/yu-posts-table.php (modified) (5 diffs)
-
includes/yu-process.php (modified) (2 diffs)
-
includes/yu-settings.php (modified) (4 diffs)
-
readme.txt (modified) (2 diffs)
-
year-updater.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
year-updater/trunk/includes/yu-posts-table.php
r2911954 r3004128 1 1 <?php 2 3 namespace YearUpdater; 2 4 3 5 if ( ! defined('ABSPATH')) { … … 5 7 } 6 8 7 class YU_Posts_Table extends WP_List_Table {9 class YU_Posts_Table extends \WP_List_Table { 8 10 private static $is_updating = false; 9 11 … … 35 37 } 36 38 39 public function display() { 40 ?> 41 <h1><?php _e('Year Updater', 'year-updater'); ?></h1> 42 <h2><?php _e('Queried Posts With Year in Title:', 'year-updater'); ?></h2> 43 <p><?php _e('Note: Posts with the current year in their title will be skipped during the process.', 'year-updater'); ?></p> 44 <?php 45 46 parent::display(); 47 } 48 37 49 public function column_default($item, $column_name) { 38 50 switch ($column_name) { … … 66 78 } 67 79 68 public function extra_tablenav($which) {69 if ($which === 'top') {70 ?>71 <div class="alignleft actions">72 <label class="screen-reader-text" for="post-search-input">Search Posts:</label>73 <input type="search" id="post-search-input" name="s" value="">74 <input type="submit" id="search-submit" class="button" value="Search Posts">75 </div>76 <?php77 }78 }79 80 80 public function prepare_items() { 81 81 $this->_column_headers = [$this->get_columns(), [], []]; 82 83 $post_type = isset($_REQUEST['post_type']) ? sanitize_text_field($_REQUEST['post_type']) : '';82 83 $post_type = $this->post_type; 84 84 $per_page = $this->get_items_per_page('posts_per_page'); 85 85 $current_page = $this->get_pagenum(); 86 86 $offset = ($current_page - 1) * $per_page; 87 87 88 add_filter('posts_where', [$this, 'yu_posts_where'], 10, 2); 89 88 90 $args = [ 89 91 'posts_per_page' => $per_page, … … 91 93 'post_status' => 'publish', 92 94 'offset' => $offset, 93 'yu_query_mode' => true94 95 ]; 95 96 $query = new WP_Query($args); 97 98 $this->items = $query->posts; 99 100 $total_items = $query->found_posts; 96 97 $query = new \WP_Query($args); 98 99 $posts_with_year = array_filter($query->posts, function($post) { 100 return preg_match('/\b\d{4}\b/', $post->post_title); 101 }); 102 103 $this->items = $posts_with_year; 104 105 $total_items = count($posts_with_year); 101 106 $this->set_pagination_args([ 102 107 'total_items' => $total_items, -
year-updater/trunk/includes/yu-process.php
r2911954 r3004128 1 1 <?php 2 3 namespace YearUpdater; 2 4 3 5 if ( ! defined( 'ABSPATH' ) ) { … … 13 15 14 16 public function update_year($post_type) { 15 // Get the current year16 17 $new_year = $this->year; 17 18 // Get an array of all posts of the given post type with year in title 18 19 19 $args = array( 20 ' numberposts' => -1,21 'post_ type' => $post_type,22 'post _status' => 'publish',20 'post_type' => $post_type, 21 'post_status' => 'publish', 22 'posts_per_page' => -1 23 23 ); 24 24 25 $ posts = get_posts($args);26 27 if (! is_array($posts)) {28 return new WP_Error('get_posts_failed', __('Failed to get posts.', 'year-updater'));25 $query = new \WP_Query($args); 26 27 if (!$query->have_posts()) { 28 return new \WP_Error('no_posts', __('No posts found to update.', 'year-updater')); 29 29 } 30 31 $year_posts = array(); 32 foreach ($posts as $post) { 33 $title = $post->post_title; 34 if (preg_match('/\b\d{4}\b/', $title)) { 35 $year_posts[] = $post; 36 } 37 } 38 39 // Update the year in the title and modified date for each post 40 foreach ($year_posts as $post) { 41 $post_id = $post->ID; 42 $title = $post->post_title; 43 44 // If the current year is in the title, skip this post 45 if (strpos($title, $this->year) !== false) { 30 31 while ($query->have_posts()) { 32 $query->the_post(); 33 $post_id = get_the_ID(); 34 $title = get_the_title(); 35 36 if (strpos($title, $this->year) !== false || !preg_match('/\b20\d\d\b/', $title)) { 46 37 continue; 47 38 } 48 49 // Find a four-digit number (assumed to be a year) in the title and replace it with the new year 50 $updated_title = preg_replace('/\b\d{4}\b/', $new_year, $title); 51 52 $current_date = current_time('mysql'); 53 54 $updated_post = array( 55 'ID' => $post_id, 56 'post_title' => $updated_title, 57 'post_modified' => $current_date, 58 'post_modified_gmt' => get_gmt_from_date($current_date) 59 ); 60 61 $result = wp_update_post($updated_post, true); 62 63 if (is_wp_error($result) || 0 === $result) { 64 return new WP_Error('update_failed', __('Failed to update post.', 'year-updater')); 65 } 66 67 // Update the "year_updated" postmeta field with the new year 39 40 $updated_title = preg_replace('/\b20\d\d\b/', $new_year, $title); 41 42 wp_update_post([ 43 'ID' => $post_id, 44 'post_title' => $updated_title 45 ]); 46 68 47 update_post_meta($post_id, 'year_updated', $new_year); 69 48 } 70 49 50 wp_reset_postdata(); 51 71 52 return true; 72 53 } 54 73 55 } -
year-updater/trunk/includes/yu-settings.php
r2911954 r3004128 1 1 <?php 2 2 3 if ( ! defined( 'ABSPATH' ) ) { 3 namespace YearUpdater; 4 5 if (!defined('ABSPATH')) { 4 6 exit; // Exit if accessed directly 5 7 } 6 8 7 if (!class_exists('WP_List_Table')){9 if (!class_exists('WP_List_Table')) { 8 10 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); 9 11 } … … 11 13 class YU_Settings { 12 14 public function __construct() { 13 add_action('admin_menu', [$this, 'register_settings_page']); 14 add_action('admin_post_yu_update', [$this, 'handle_form_submission']); 15 add_action('admin_init', [$this, 'handle_form_submission']); 15 add_action('admin_menu', array($this, 'register_settings_page')); 16 add_action('admin_post_yu_update', array($this, 'handle_form_submission')); 16 17 } 17 18 18 19 public function register_settings_page() { 19 20 add_menu_page( 20 __( 'Year Updater', 'year-updater'),21 __( 'Year Updater', 'year-updater'),21 __('Year Updater', 'year-updater'), 22 __('Year Updater', 'year-updater'), 22 23 'manage_options', 23 24 'year-updater', 24 [$this, 'display_settings_page'],25 array($this, 'display_settings_page'), 25 26 'dashicons-calendar' 26 ); 27 ); 27 28 } 28 29 29 30 public function display_settings_page() { 30 31 $this->display_notices(); 31 32 if (isset($_GET['post_type'])) { 33 $this->display_queried_posts($_GET['post_type']); 34 } elseif (isset($_POST['submit']) && $_POST['submit'] === 'Query Posts' && isset($_POST['post_type'])) { 35 $this->display_queried_posts($_POST['post_type']); 32 33 if (!current_user_can('manage_options')) { 34 wp_die(__('You do not have sufficient permissions to access this page.', 'year-updater')); 35 } 36 37 if (isset($_GET['post_type']) && !empty($_GET['post_type'])) { 38 $this->display_queried_posts(sanitize_text_field($_GET['post_type'])); 36 39 } else { 37 40 $this->display_form(); … … 40 43 41 44 private function display_form() { 42 if ( !current_user_can( 'manage_options' )) {43 wp_die( __('You do not have sufficient permissions to access this page.' ));44 }45 46 45 ?> 47 46 <div class="wrap"> 48 <h1>Year Updater</h1> 49 <form method="post" action="<?php echo admin_url('admin.php?page=year-updater'); ?>"> 50 <label for="post_type">Select Post Type:</label> 47 <h1><?php echo esc_html(get_admin_page_title()); ?></h1> 48 <form method="get" action="<?php echo esc_url(admin_url('admin.php')); ?>"> 49 <input type="hidden" name="page" value="year-updater"> 50 <label for="post_type"><?php _e('Select Post Type:', 'year-updater'); ?></label> 51 51 <select id="post_type" name="post_type"> 52 52 <?php 53 53 $post_types = get_post_types(); 54 54 foreach ($post_types as $post_type) { 55 printf('<option value="%s">%s</option>', $post_type, $post_type);55 echo '<option value="' . esc_attr($post_type) . '">' . esc_html($post_type) . '</option>'; 56 56 } 57 57 ?> 58 58 </select> 59 < input type="submit" name="submit" value="Query Posts">59 <?php submit_button(__('Search Posts', 'year-updater')); ?> 60 60 </form> 61 61 </div> … … 64 64 65 65 private function display_queried_posts($post_type) { 66 require_once YU_PLUGIN_PATH . 'includes/yu-posts-table.php'; 67 $posts_list_table = new YU_Posts_Table(); 68 $posts_list_table->prepare_items(); 66 $args = array( 67 'post_type' => $post_type, 68 'post_status' => 'publish', 69 'posts_per_page' => -1, 70 ); 71 72 $query = new \WP_Query($args); 73 74 // Filter posts that have a year in the title 75 $posts_with_year = array_filter($query->posts, function($post) { 76 return preg_match('/\b\d{4}\b/', $post->post_title); 77 }); 78 79 $posts_table = new YU_Posts_Table(); 80 81 $posts_table->items = $posts_with_year; 82 83 $posts_table->prepare_items(); 84 85 ob_start(); 69 86 ?> 70 <div class="wrap"> 71 <h1>Year Updater</h1> 72 <h2>Queried Posts With Year in Title:</h2> 73 <p>Note: Posts with the current year in their title will be skipped during the process.</p> 74 <form method="post" action="<?php echo admin_url('admin-post.php'); ?>"> 75 <input type="hidden" name="action" value="yu_update"> 76 <input type="hidden" name="post_type" value="<?php echo esc_attr($post_type); ?>"> 77 <?php wp_nonce_field('yu_update_action', 'yu_nonce_field'); ?> 78 <?php $posts_list_table->display(); ?> 79 <input type="submit" name="submit" value="Update Posts"> 80 </form> 81 </div> 87 <form method="post" action="<?php echo admin_url('admin-post.php'); ?>"> 88 <input type="hidden" name="action" value="yu_update"> 89 <input type="hidden" name="post_type" value="<?php echo esc_attr($post_type); ?>"> 90 <?php wp_nonce_field('yu_update_action', 'yu_nonce_field'); ?> 91 92 <?php 93 $posts_table->display(); 94 ?> 95 96 <input type="submit" name="submit" class="button-primary" value="<?php _e('Update Posts', 'year-updater'); ?>"> 97 </form> 82 98 <?php 83 } 99 echo ob_get_clean(); 100 101 wp_reset_postdata(); 102 } 103 84 104 85 105 public function handle_form_submission() { -
year-updater/trunk/readme.txt
r2911957 r3004128 3 3 Tags: title, year, updater 4 4 Requires at least: 4.7 5 Tested up to: 6. 26 Stable tag: 1. 2.07 Requires PHP: 7.45 Tested up to: 6.4.1 6 Stable tag: 1.3.0 7 Requires PHP: 8.1 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 40 40 == Changelog == 41 41 42 = 1.3.0 = 43 * Organized the main plugin file for better readability and maintenance. 44 * Refined the method for updating the year in post titles. 45 * Introduced namespace usage for better code organization and to avoid name conflicts. 46 * Modified the approach of querying posts to enhance performance, particularly for large datasets 47 * Minor code improvements 48 42 49 = 1.2.0 = 43 50 * Better and faster code implementation -
year-updater/trunk/year-updater.php
r2911954 r3004128 4 4 Plugin URI: https://nabaleka.com 5 5 Description: This plugin allows you to update the year in the titles of your posts to the current year. 6 Version: 1. 2.06 Version: 1.3.0 7 7 Author: Ammanulah Emmanuel 8 8 Author URI: https://nabaleka.com 9 9 Text Domain: year-updater 10 License: GPL 3.010 License: GPL2.0 11 11 */ 12 13 namespace YearUpdater; 12 14 13 15 if (!defined('ABSPATH')) { … … 15 17 } 16 18 19 // Constants 17 20 define('YU_PLUGIN_PATH', plugin_dir_path(__FILE__)); 18 21 define('YU_PLUGIN_URL', plugin_dir_url(__FILE__)); 19 define('YU_VERSION', '1. 2.0');22 define('YU_VERSION', '1.3.0'); 20 23 21 require_once YU_PLUGIN_PATH . 'yu-core.php'; 22 require_once YU_PLUGIN_PATH . 'includes/yu-cli-command.php'; 24 // Dependencies 25 require_once YU_PLUGIN_PATH . 'includes/yu-settings.php'; 26 require_once YU_PLUGIN_PATH . 'includes/yu-process.php'; 27 require_once YU_PLUGIN_PATH . 'includes/yu-posts-table.php'; 23 28 24 29 class Year_Updater_Main { 25 30 26 31 public function __construct() { 27 $this->year_updater = new YU_Core(); 28 $this->year_updater->register_hooks(); 32 $this->register_hooks(); 33 $this->year_updater_settings = new YU_Settings(); 34 $this->year_updater_process = new YU_Process(); 29 35 } 30 36 31 public function load_textdomain() { 37 public function register_hooks() { 38 add_action('admin_enqueue_scripts', array($this, 'enqueue_assets')); 32 39 load_plugin_textdomain('year-updater', false, basename(dirname(__FILE__)) . '/languages'); 40 } 41 42 public function enqueue_assets() { 43 wp_enqueue_style('year-updater', YU_PLUGIN_URL . 'assets/css/yu-styles.css', array(), YU_VERSION); 44 wp_enqueue_script('year-updater', YU_PLUGIN_URL . 'assets/js/yu-scripts.js', array('jquery'), YU_VERSION, true); 33 45 } 34 46 } 35 47 36 function init_year_updater() {37 $year_updater_main = new Year_Updater_Main();38 return $year_updater_main;39 }40 41 48 // Initialize the plugin 42 global $year_updater_main; 43 $year_updater_main = init_year_updater(); 44 45 // Load plugin text domain 46 add_action('plugins_loaded', array($year_updater_main, 'load_textdomain')); 49 $year_updater_main = new Year_Updater_Main();
Note: See TracChangeset
for help on using the changeset viewer.