close

Plugin Directory

Changeset 3435421


Ignore:
Timestamp:
01/08/2026 06:32:27 PM (4 months ago)
Author:
xavivars
Message:

Decouple settings and quote migration

Location:
xv-random-quotes/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • xv-random-quotes/trunk/changelog.txt

    r3435038 r3435421  
    11== XV Random Quotes ==
     2
     3= 2.6.0 =
     4Improvement: Default category migration from legacy settings
     5Improvement: Decouple settings migration steps
    26
    37= 2.5.0 =
  • xv-random-quotes/trunk/readme.txt

    r3435038 r3435421  
    66Tested up to: 6.9
    77Requires PHP: 7.4
    8 Stable tag: 2.5.0
     8Stable tag: 2.6.0
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • xv-random-quotes/trunk/src/Admin/MetaBoxes.php

    r3430809 r3435421  
    3131        add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );
    3232        add_action( 'save_post_xv_quote', array( $this, 'save_all_meta_boxes' ), 10, 2 );
     33        add_action( 'save_post_xv_quote', array( $this, 'apply_default_category' ), 20, 1 );
    3334    }
    3435
     
    7475        return $this->get_editor_settings( array(
    7576            'teeny'         => false,
    76             'rows'          => 8,
     77            'textarea_rows' => 10,
    7778            'media_buttons' => true,
    7879            'quicktags'     => true,
     
    8687     *
    8788     * @since 2.0.0
    88      * @param array $options Number of textarea rows. Default 8.
     89     * @param array $options Editor options to override defaults.
    8990     * @return array Editor settings
    9091     */
    9192    private function get_editor_settings( $options = array() ) {
    92         return array_merge(
    93             array(
    94                 'media_buttons' => false,          // No "Add Media" button
    95                 'quicktags'     => false,          // No HTML/Text tab
    96                 'teeny'         => true,           // Minimal editor
    97                 'textarea_rows' => 1,              // Single line textarea
    98                 'tinymce'       => array(
    99                     'toolbar1' => 'bold,italic,link,unlink',  // Only these buttons
    100                     'toolbar2' => '',                         // No second toolbar
    101                     'toolbar3' => '',                         // No third toolbar
    102                 ),
     93        $defaults = array(
     94            'media_buttons' => false,          // No "Add Media" button
     95            'quicktags'     => false,          // No HTML/Text tab
     96            'teeny'         => true,           // Minimal editor
     97            'textarea_rows' => 1,              // Single line textarea
     98            'tinymce'       => array(
     99                'toolbar1' => 'bold,italic,link,unlink',  // Only these buttons
     100                'toolbar2' => '',                         // No second toolbar
     101                'toolbar3' => '',                         // No third toolbar
    103102            ),
    104             $options
    105         );
     103        );
     104       
     105        return array_merge( $defaults, $options );
    106106    }
    107107
     
    230230        }
    231231    }
     232
     233    /**
     234     * Apply default category to quotes without any category assigned
     235     *
     236     * Automatically assigns the default category term (from v1.x migration)
     237     * to quotes that don't have any quote_category term assigned.
     238     *
     239     * @since 2.0.0
     240     * @param int $post_id The post ID.
     241     */
     242    public function apply_default_category( $post_id ) {
     243        // Only apply to xv_quote post type
     244        if ( get_post_type( $post_id ) !== 'xv_quote' ) {
     245            return;
     246        }
     247
     248        // Check if post has any quote_category terms assigned
     249        $terms = wp_get_post_terms( $post_id, 'quote_category', array( 'fields' => 'ids' ) );
     250
     251        // If no terms assigned, try to assign the default
     252        if ( empty( $terms ) ) {
     253            $default_term_id = (int) get_option( 'xv_quotes_default_category_id', 0 );
     254
     255            if ( $default_term_id > 0 ) {
     256                // Verify the term exists
     257                if ( get_term( $default_term_id, 'quote_category' ) ) {
     258                    wp_set_post_terms( $post_id, array( $default_term_id ), 'quote_category', false );
     259                }
     260            }
     261        }
     262    }
    232263}
     264
  • xv-random-quotes/trunk/src/Migration/SettingsMigrator.php

    r3429335 r3435421  
    1515 * Handles migration of settings from legacy stray_quotes_options
    1616 * to individual xv_quotes_* options.
     17 *
     18 * Uses version numbers for the _xv_quotes_migrated option:
     19 * - 0 or false: Not migrated
     20 * - 1: Settings migrated (v1 - initial migration)
     21 * - 2: Default category setup (v2 - added later)
     22 * - etc.
    1723 */
    1824class SettingsMigrator {
    1925
    2026    /**
     27     * Current migration version
     28     *
     29     * @var int
     30     */
     31    const MIGRATION_VERSION = 2;
     32
     33    /**
    2134     * Run the migration
    2235     */
    2336    public static function migrate() {
    24         // Check if migration has already been run
    25         if ( get_option( '_xv_quotes_migrated', false ) ) {
    26             return;
    27         }
    28 
     37        // Get current migration version (0 if never migrated, or false for backward compat)
     38        $current_version = (int) get_option( '_xv_quotes_migrated', 0 );
     39
     40        // Run v1 migration if not yet done
     41        if ( $current_version < 1 ) {
     42            self::migrate_v200();
     43        }
     44
     45        // Run v2 migration if not yet done
     46        if ( $current_version < 2 ) {
     47            self::migrate_v260();
     48        }
     49
     50        // Update version to current
     51        update_option( '_xv_quotes_migrated', self::MIGRATION_VERSION );
     52    }
     53
     54    /**
     55     * V2.0.0 Migration: Settings migration from legacy format
     56     */
     57    private static function migrate_v200() {
    2958        // Get old settings
    3059        $old_settings = get_option( 'stray_quotes_options', array() );
     
    4069            self::migrate_from_legacy_settings( $old_settings );
    4170        }
    42 
    43         // Mark migration as complete
    44         update_option( '_xv_quotes_migrated', true );
     71    }
     72
     73    /**
     74     * V2.6.0 Migration: Default category setup
     75     */
     76    private static function migrate_v260() {
     77        // Check if there's a default category to migrate
     78        $old_settings = get_option( 'stray_quotes_options', array() );
     79
     80        if ( isset( $old_settings['stray_default_category'] ) && ! empty( $old_settings['stray_default_category'] ) ) {
     81            self::create_default_category_term( $old_settings['stray_default_category'] );
     82        }
    4583    }
    4684
     
    109147            'stray_after_loader'  => Settings::OPTION_AFTER_LOADER,
    110148            'stray_loading'       => Settings::OPTION_LOADING,
     149
     150            // Default category (for applying to migrated quotes without a category)
     151            'stray_default_category' => 'xv_quotes_default_category',
    111152        );
    112153
     
    146187        }
    147188    }
     189
     190    /**
     191     * Create the default category term in the quote_category taxonomy
     192     *
     193     * @param string $category_name The name of the default category.
     194     */
     195    private static function create_default_category_term( $category_name ) {
     196        $taxonomy = 'quote_category';
     197
     198        // Check if term already exists
     199        $term = term_exists( $category_name, $taxonomy );
     200
     201        if ( ! $term ) {
     202            // Create the term
     203            $term = wp_insert_term( $category_name, $taxonomy );
     204
     205            if ( is_wp_error( $term ) ) {
     206                return;
     207            }
     208        }
     209
     210        // Get the term ID (term_exists returns int, wp_insert_term returns array)
     211        $term_id = is_array( $term ) ? $term['term_id'] : $term;
     212
     213        // Store the term ID for use as default when creating new quotes
     214        update_option( 'xv_quotes_default_category_id', (int) $term_id );
     215    }
    148216}
  • xv-random-quotes/trunk/vendor/composer/installed.php

    r3434614 r3435421  
    44        'pretty_version' => 'dev-main',
    55        'version' => 'dev-main',
    6         'reference' => 'fbcdfb5200202cd5c3c489ec669901a9e8531432',
     6        'reference' => 'c5c63fb91808ddbfdfb9ab496437bfe15ad21871',
    77        'type' => 'wordpress-plugin',
    88        'install_path' => __DIR__ . '/../../',
     
    1414            'pretty_version' => 'dev-main',
    1515            'version' => 'dev-main',
    16             'reference' => 'fbcdfb5200202cd5c3c489ec669901a9e8531432',
     16            'reference' => 'c5c63fb91808ddbfdfb9ab496437bfe15ad21871',
    1717            'type' => 'wordpress-plugin',
    1818            'install_path' => __DIR__ . '/../../',
  • xv-random-quotes/trunk/xv-random-quotes.php

    r3435038 r3435421  
    55Author: Xavi Ivars
    66Author URI: https://xavi.ivars.me/
    7 Version: 2.5.0
     7Version: 2.6.0
    88Requires at least: 6.0
    99Requires PHP: 7.4
     
    9090 */
    9191function xv_quotes_check_migration() {
    92     // If the 'migrated' option doesn't exist (returns false), run the migration
    93     // Also check that migration hasn't already been triggered or isn't pending
    94     if ( ! get_option( 'xv_quotes_migrated_v2' )
    95          && ! get_option( 'xv_quotes_needs_migration' )
    96          && ! get_option( 'xv_migration_pending' ) ) {
    97         xv_quotes_activation_migration();
    98     }
     92    // Run settings migration (if not yet done)
     93    // This handles both v1 settings migration and v2 default category setup
     94    if ( class_exists( '\XVRandomQuotes\Migration\SettingsMigrator' ) ) {
     95        $current_version = (int) get_option( '_xv_quotes_migrated', 0 );
     96        if ( $current_version < \XVRandomQuotes\Migration\SettingsMigrator::MIGRATION_VERSION ) {
     97            \XVRandomQuotes\Migration\SettingsMigrator::migrate();
     98        }
     99    }
     100
     101    // Check if quote migration is needed
     102    // If the 'migrated' option doesn't exist (returns false), run the migration
     103    // Also check that migration hasn't already been triggered or isn't pending
     104    if ( ! get_option( 'xv_quotes_migrated_v2' )
     105         && ! get_option( 'xv_quotes_needs_migration' )
     106         && ! get_option( 'xv_migration_pending' ) ) {
     107        xv_quotes_activation_migration();
     108    }
    99109}
    100110add_action( 'admin_init', 'xv_quotes_check_migration' );
Note: See TracChangeset for help on using the changeset viewer.