Plugin Directory

Changeset 3350053


Ignore:
Timestamp:
08/26/2025 05:24:35 AM (7 months ago)
Author:
infoforte
Message:

1.6.0

  • Bug fixes and other improvements
  • Added batch update functionality
Location:
lyxity/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lyxity/trunk/lyxity.php

    r3348958 r3350053  
    33Plugin Name: Lyxity
    44Description: The art of modern search engine optimization
    5 Version: 1.5.0
     5Version: 1.6.0
    66Author: Infoforte
    77Author URI: https://infoforte.com
     
    2020
    2121// Define plugin constants
    22 define('LYXITY_VERSION', '1.5.0');
     22define('LYXITY_VERSION', '1.6.0');
    2323define('LYXITY_FILE', __FILE__);
    2424define('LYXITY_PATH', plugin_dir_path(__FILE__));
     
    6767        add_action('wp_ajax_lyxity_schedule_bulk_update', array($this, 'ajax_schedule_bulk_update'));
    6868        add_action('wp_ajax_lyxity_schedule_bulk_rewrite', array($this, 'ajax_schedule_bulk_rewrite'));
     69        // Background processing endpoints (non-blocking triggers)
     70        add_action('wp_ajax_lyxity_process_bulk_update', array($this, 'ajax_process_bulk_update'));
     71        add_action('wp_ajax_nopriv_lyxity_process_bulk_update', array($this, 'ajax_process_bulk_update'));
     72        add_action('wp_ajax_lyxity_process_bulk_rewrite', array($this, 'ajax_process_bulk_rewrite'));
     73        add_action('wp_ajax_nopriv_lyxity_process_bulk_rewrite', array($this, 'ajax_process_bulk_rewrite'));
    6974       
    7075        // Add AJAX handlers for settings
     
    16331638        }
    16341639        error_log('Lyxity Debug - ajax_schedule_bulk_update - Post titles: ' . json_encode($post_titles));
    1635 
    1636         // Schedule the cron job
    1637         $scheduled = wp_schedule_single_event(time(), 'lyxity_bulk_update_cron', array($post_ids));
    1638        
    1639         if ($scheduled === false) {
    1640             error_log('Lyxity Debug - ajax_schedule_bulk_update - Failed to schedule cron job');
    1641             wp_send_json_error('Failed to schedule bulk update');
    1642             return;
    1643         }
    1644        
    1645         error_log('Lyxity Debug - ajax_schedule_bulk_update - Successfully scheduled bulk update for ' . count($post_ids) . ' posts');
    1646        
     1640        // Split into small chunks and dispatch a background request per chunk
     1641        $batch_size = 5; // process 5 posts per background request
     1642        $batches = array_chunk($post_ids, $batch_size);
     1643
     1644        foreach ($batches as $index => $batch) {
     1645            $token = wp_generate_password(20, false);
     1646            set_transient('lyxity_async_' . $token, array(
     1647                'type' => 'update',
     1648                'post_ids' => $batch,
     1649                'created' => time(),
     1650                'batch_index' => $index,
     1651                'batch_size' => count($batch),
     1652                'total_batches' => count($batches),
     1653            ), 15 * MINUTE_IN_SECONDS);
     1654
     1655            $url = add_query_arg(array(
     1656                'action' => 'lyxity_process_bulk_update',
     1657                'token'  => rawurlencode($token),
     1658            ), admin_url('admin-ajax.php'));
     1659
     1660            // Fire-and-forget per batch
     1661            wp_remote_post($url, array(
     1662                'timeout'   => 1,
     1663                'blocking'  => false,
     1664                'sslverify' => true,
     1665                'headers'   => array('Content-Type' => 'application/x-www-form-urlencoded'),
     1666            ));
     1667        }
     1668
     1669        error_log('Lyxity Debug - ajax_schedule_bulk_update - Dispatched ' . count($batches) . ' background batches of size ' . $batch_size);
     1670
    16471671        wp_send_json_success(array(
    16481672            'message' => sprintf(
    1649                 /* translators: %d: number of posts scheduled for bulk update */
    1650                 esc_html__('Scheduled bulk update for %d posts', 'lyxity'),
    1651                 count($post_ids)
     1673                /* translators: 1: posts, 2: batches */
     1674                esc_html__('Triggered background update for %1$d posts in %2$d batches', 'lyxity'),
     1675                count($post_ids),
     1676                count($batches)
    16521677            )
    16531678        ));
     
    16941719        }
    16951720        error_log('Lyxity Debug - ajax_schedule_bulk_rewrite - Post titles: ' . json_encode($post_titles));
    1696 
    1697         // Schedule the cron job
    1698         $scheduled = wp_schedule_single_event(time(), 'lyxity_bulk_rewrite_cron', array($post_ids));
    1699        
    1700         if ($scheduled === false) {
    1701             error_log('Lyxity Debug - ajax_schedule_bulk_rewrite - Failed to schedule cron job');
    1702             wp_send_json_error('Failed to schedule bulk rewrite');
    1703             return;
    1704         }
    1705        
    1706         error_log('Lyxity Debug - ajax_schedule_bulk_rewrite - Successfully scheduled bulk rewrite for ' . count($post_ids) . ' posts');
    1707        
     1721        // Split into small chunks and dispatch a background request per chunk
     1722        $batch_size = 5; // process 5 posts per background request
     1723        $batches = array_chunk($post_ids, $batch_size);
     1724
     1725        foreach ($batches as $index => $batch) {
     1726            $token = wp_generate_password(20, false);
     1727            set_transient('lyxity_async_' . $token, array(
     1728                'type' => 'rewrite',
     1729                'post_ids' => $batch,
     1730                'created' => time(),
     1731                'batch_index' => $index,
     1732                'batch_size' => count($batch),
     1733                'total_batches' => count($batches),
     1734            ), 15 * MINUTE_IN_SECONDS);
     1735
     1736            $url = add_query_arg(array(
     1737                'action' => 'lyxity_process_bulk_rewrite',
     1738                'token'  => rawurlencode($token),
     1739            ), admin_url('admin-ajax.php'));
     1740
     1741            // Fire-and-forget per batch
     1742            wp_remote_post($url, array(
     1743                'timeout'   => 1,
     1744                'blocking'  => false,
     1745                'sslverify' => true,
     1746                'headers'   => array('Content-Type' => 'application/x-www-form-urlencoded'),
     1747            ));
     1748        }
     1749
     1750        error_log('Lyxity Debug - ajax_schedule_bulk_rewrite - Dispatched ' . count($batches) . ' background batches of size ' . $batch_size);
     1751
    17081752        wp_send_json_success(array(
    17091753            'message' => sprintf(
    1710                 /* translators: %d: number of posts scheduled for bulk rewrite */
    1711                 esc_html__('Scheduled bulk rewrite for %d posts', 'lyxity'),
    1712                 count($post_ids)
     1754                /* translators: 1: posts, 2: batches */
     1755                esc_html__('Triggered background enhance for %1$d posts in %2$d batches', 'lyxity'),
     1756                count($post_ids),
     1757                count($batches)
    17131758            )
    17141759        ));
    1715     }
    1716 
     1760
     1761    }
     1762
     1763    /**
     1764     * Internal background processor: Bulk Update
     1765     * Accepts a one-time token referencing a transient created by the scheduler.
     1766     */
     1767    public function ajax_process_bulk_update() {
     1768        $token = isset($_REQUEST['token']) ? sanitize_text_field(wp_unslash($_REQUEST['token'])) : '';
     1769        if (empty($token)) {
     1770            wp_send_json_error('Missing token', 400);
     1771        }
     1772
     1773        $payload = get_transient('lyxity_async_' . $token);
     1774        if (!$payload || !is_array($payload) || !isset($payload['post_ids']) || $payload['type'] !== 'update') {
     1775            wp_send_json_error('Invalid or expired token', 400);
     1776        }
     1777        // Invalidate immediately to prevent re-use
     1778        delete_transient('lyxity_async_' . $token);
     1779
     1780        $post_ids = array_map('intval', (array) $payload['post_ids']);
     1781        if (empty($post_ids)) {
     1782            wp_send_json_error('No posts to process', 400);
     1783        }
     1784
     1785        // Run the existing bulk update processor
     1786        $this->process_bulk_update($post_ids);
     1787
     1788        wp_send_json_success(array('processed' => count($post_ids)));
     1789    }
     1790
     1791    /**
     1792     * Internal background processor: Bulk Rewrite
     1793     * Accepts a one-time token referencing a transient created by the scheduler.
     1794     */
     1795    public function ajax_process_bulk_rewrite() {
     1796        $token = isset($_REQUEST['token']) ? sanitize_text_field(wp_unslash($_REQUEST['token'])) : '';
     1797        if (empty($token)) {
     1798            wp_send_json_error('Missing token', 400);
     1799        }
     1800
     1801        $payload = get_transient('lyxity_async_' . $token);
     1802        if (!$payload || !is_array($payload) || !isset($payload['post_ids']) || $payload['type'] !== 'rewrite') {
     1803            wp_send_json_error('Invalid or expired token', 400);
     1804        }
     1805        // Invalidate immediately to prevent re-use
     1806        delete_transient('lyxity_async_' . $token);
     1807
     1808        $post_ids = array_map('intval', (array) $payload['post_ids']);
     1809        if (empty($post_ids)) {
     1810            wp_send_json_error('No posts to process', 400);
     1811        }
     1812
     1813        // Run the existing bulk rewrite processor
     1814        $this->process_bulk_rewrite($post_ids);
     1815
     1816        wp_send_json_success(array('processed' => count($post_ids)));
     1817    }
    17171818    /**
    17181819     * Process bulk update cron job
  • lyxity/trunk/readme.txt

    r3348958 r3350053  
    44Requires at least: 5.0
    55Tested up to: 6.8
    6 Stable tag: 1.5.0
     6Stable tag: 1.6.0
    77Requires PHP: 7.0
    88License: GPLv2 or later
     
    113113
    114114== Changelog ==
     115= 1.6.0 =
     116- Bug fixes and other improvements
     117- Added batch update functionality
     118
    115119= 1.5.0 =
    116120- Bug fixes and other improvements
Note: See TracChangeset for help on using the changeset viewer.