Plugin Directory

Changeset 3004128


Ignore:
Timestamp:
12/01/2023 09:38:41 AM (2 years ago)
Author:
smartkenyan
Message:

Version 1.3.0

Location:
year-updater/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • year-updater/trunk/includes/yu-posts-table.php

    r2911954 r3004128  
    11<?php
     2
     3namespace YearUpdater;
    24
    35if ( ! defined('ABSPATH')) {
     
    57}
    68
    7 class YU_Posts_Table extends WP_List_Table {
     9class YU_Posts_Table extends \WP_List_Table {
    810    private static $is_updating = false;
    911
     
    3537    }
    3638
     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   
    3749    public function column_default($item, $column_name) {
    3850        switch ($column_name) {
     
    6678    }
    6779
    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             <?php
    77         }
    78     }
    79 
    8080    public function prepare_items() {
    8181        $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;
    8484        $per_page = $this->get_items_per_page('posts_per_page');
    8585        $current_page = $this->get_pagenum();
    8686        $offset = ($current_page - 1) * $per_page;
    87    
     87
     88        add_filter('posts_where', [$this, 'yu_posts_where'], 10, 2);
     89       
    8890        $args = [
    8991            'posts_per_page' => $per_page,
     
    9193            'post_status' => 'publish',
    9294            'offset'      => $offset,
    93             'yu_query_mode' => true
    9495        ];
    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);
    101106        $this->set_pagination_args([
    102107            'total_items' => $total_items,
  • year-updater/trunk/includes/yu-process.php

    r2911954 r3004128  
    11<?php
     2
     3namespace YearUpdater;
    24
    35if ( ! defined( 'ABSPATH' ) ) {
     
    1315
    1416    public function update_year($post_type) {
    15         // Get the current year
    1617        $new_year = $this->year;
    17 
    18         // Get an array of all posts of the given post type with year in title
     18   
    1919        $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
    2323        );
    2424   
    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'));
    2929        }
    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)) {
    4637                continue;
    4738            }
    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   
    6847            update_post_meta($post_id, 'year_updated', $new_year);
    6948        }
    70 
     49   
     50        wp_reset_postdata();
     51   
    7152        return true;
    7253    }
     54   
    7355}
  • year-updater/trunk/includes/yu-settings.php

    r2911954 r3004128  
    11<?php
    22
    3 if ( ! defined( 'ABSPATH' ) ) {
     3namespace YearUpdater;
     4
     5if (!defined('ABSPATH')) {
    46    exit; // Exit if accessed directly
    57}
    68
    7 if(!class_exists('WP_List_Table')){
     9if (!class_exists('WP_List_Table')) {
    810    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    911}
     
    1113class YU_Settings {
    1214    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'));
    1617    }
    1718
    1819    public function register_settings_page() {
    1920        add_menu_page(
    20             __( 'Year Updater', 'year-updater' ),
    21             __( 'Year Updater', 'year-updater' ),
     21            __('Year Updater', 'year-updater'),
     22            __('Year Updater', 'year-updater'),
    2223            'manage_options',
    2324            'year-updater',
    24             [$this, 'display_settings_page'],
     25            array($this, 'display_settings_page'),
    2526            'dashicons-calendar'
    26         ); 
     27        );
    2728    }
    2829
    2930    public function display_settings_page() {
    3031        $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']));
    3639        } else {
    3740            $this->display_form();
     
    4043
    4144    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 
    4645        ?>
    4746        <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>
    5151                <select id="post_type" name="post_type">
    5252                    <?php
    5353                    $post_types = get_post_types();
    5454                    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>';
    5656                    }
    5757                    ?>
    5858                </select>
    59                 <input type="submit" name="submit" value="Query Posts">
     59                <?php submit_button(__('Search Posts', 'year-updater')); ?>
    6060            </form>
    6161        </div>
     
    6464
    6565    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();
    6986        ?>
    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>
    8298        <?php
    83     }   
     99        echo ob_get_clean();
     100   
     101        wp_reset_postdata();
     102    }
     103   
    84104   
    85105    public function handle_form_submission() {
  • year-updater/trunk/readme.txt

    r2911957 r3004128  
    33Tags: title, year, updater
    44Requires at least: 4.7
    5 Tested up to: 6.2
    6 Stable tag: 1.2.0
    7 Requires PHP: 7.4
     5Tested up to: 6.4.1
     6Stable tag: 1.3.0
     7Requires PHP: 8.1
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    4040== Changelog ==
    4141
     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
    4249= 1.2.0 =
    4350* Better and faster code implementation
  • year-updater/trunk/year-updater.php

    r2911954 r3004128  
    44Plugin URI: https://nabaleka.com
    55Description: This plugin allows you to update the year in the titles of your posts to the current year.
    6 Version: 1.2.0
     6Version: 1.3.0
    77Author: Ammanulah Emmanuel
    88Author URI: https://nabaleka.com
    99Text Domain: year-updater
    10 License: GPL3.0
     10License: GPL2.0
    1111*/
     12
     13namespace YearUpdater;
    1214
    1315if (!defined('ABSPATH')) {
     
    1517}
    1618
     19// Constants
    1720define('YU_PLUGIN_PATH', plugin_dir_path(__FILE__));
    1821define('YU_PLUGIN_URL', plugin_dir_url(__FILE__));
    19 define('YU_VERSION', '1.2.0');
     22define('YU_VERSION', '1.3.0');
    2023
    21 require_once YU_PLUGIN_PATH . 'yu-core.php';
    22 require_once YU_PLUGIN_PATH . 'includes/yu-cli-command.php';
     24// Dependencies
     25require_once YU_PLUGIN_PATH . 'includes/yu-settings.php';
     26require_once YU_PLUGIN_PATH . 'includes/yu-process.php';
     27require_once YU_PLUGIN_PATH . 'includes/yu-posts-table.php';
    2328
    2429class Year_Updater_Main {
    2530
    2631    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();
    2935    }
    3036
    31     public function load_textdomain() {
     37    public function register_hooks() {
     38        add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
    3239        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);
    3345    }
    3446}
    3547
    36 function init_year_updater() {
    37     $year_updater_main = new Year_Updater_Main();
    38     return $year_updater_main;
    39 }
    40 
    4148// 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.