Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![CS](https://github.com/emilia-capital/aaa-option-optimizer/actions/workflows/cs.yml/badge.svg)](https://github.com/emilia-capital/aaa-option-optimizer/actions/workflows/cs.yml)
[![PHPStan](https://github.com/Emilia-Capital/aaa-option-optimizer/actions/workflows/phpstan.yml/badge.svg)](https://github.com/Emilia-Capital/aaa-option-optimizer/actions/workflows/phpstan.yml)
[![Lint](https://github.com/emilia-capital/aaa-option-optimizer/actions/workflows/lint.yml/badge.svg)](https://github.com/emilia-capital/aaa-option-optimizer/actions/workflows/lint.yml)
[![Security](https://github.com/emilia-capital/aaa-option-optimizer/actions/workflows/security.yml/badge.svg)](https://github.com/emilia-capital/aaa-option-optimizer/actions/workflows/security.yml)
[![CS](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/cs.yml/badge.svg)](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/cs.yml)
[![PHPStan](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/phpstan.yml/badge.svg)](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/phpstan.yml)
[![Lint](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/lint.yml/badge.svg)](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/lint.yml)
[![Security](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/security.yml/badge.svg)](https://github.com/ProgressPlanner/aaa-option-optimizer/actions/workflows/security.yml)

[![WordPress Plugin Version](https://img.shields.io/wordpress/plugin/v/aaa-option-optimizer.svg)](https://wordpress.org/plugins/aaa-option-optimizer/)
![WordPress Plugin: Tested WP Version](https://img.shields.io/wordpress/plugin/tested/aaa-option-optimizer.svg)
Expand All @@ -10,7 +10,7 @@
[![WordPress Plugin Rating](https://img.shields.io/wordpress/plugin/stars/aaa-option-optimizer.svg)](https://wordpress.org/support/plugin/aaa-option-optimizer/reviews/)
[![GitHub](https://img.shields.io/github/license/ProgressPlanner/aaa-option-optimizer.svg)](https://github.com/ProgressPlanner/aaa-option-optimizer/blob/main/LICENSE)

[![Try this plugin on the WordPress playground](https://img.shields.io/badge/Try%20this%20plugin%20on%20the%20WordPress%20Playground-%23117AC9.svg?style=for-the-badge&logo=WordPress&logoColor=ddd)](https://playground.wordpress.net/#%7B%22landingPage%22:%22/wp-admin/tools.php?page=aaa-option-optimizer%22,%22features%22:%7B%22networking%22:true%7D,%22steps%22:%5B%7B%22step%22:%22defineWpConfigConsts%22,%22consts%22:%7B%22IS_PLAYGROUND_PREVIEW%22:true%7D%7D,%7B%22step%22:%22login%22,%22username%22:%22admin%22,%22password%22:%22password%22%7D,%7B%22step%22:%22installPlugin%22,%22pluginZipFile%22:%7B%22resource%22:%22url%22,%22url%22:%22https://bypass-cors.altha.workers.dev/https://github.com/Emilia-Capital/aaa-option-optimizer/archive/refs/heads/develop.zip%22%7D,%22options%22:%7B%22activate%22:true%7D%7D%5D%7D)
[![Try this plugin on the WordPress playground](https://img.shields.io/badge/Try%20this%20plugin%20on%20the%20WordPress%20Playground-%23117AC9.svg?style=for-the-badge&logo=WordPress&logoColor=ddd)](https://playground.wordpress.net/#%7B%22landingPage%22:%22/wp-admin/tools.php?page=aaa-option-optimizer%22,%22features%22:%7B%22networking%22:true%7D,%22steps%22:%5B%7B%22step%22:%22defineWpConfigConsts%22,%22consts%22:%7B%22IS_PLAYGROUND_PREVIEW%22:true%7D%7D,%7B%22step%22:%22login%22,%22username%22:%22admin%22,%22password%22:%22password%22%7D,%7B%22step%22:%22installPlugin%22,%22pluginZipFile%22:%7B%22resource%22:%22url%22,%22url%22:%22https://bypass-cors.altha.workers.dev/https://github.com/ProgressPlanner/aaa-option-optimizer/archive/refs/heads/develop.zip%22%7D,%22options%22:%7B%22activate%22:true%7D%7D%5D%7D)

![GitHub banner](/.wordpress-org/github_banner_aaaoo_pp.png)

Expand All @@ -26,7 +26,7 @@ Install this plugin, and go through your entire site. Best is to use it normally

### Why the AAA prefix in the plugin name?

Because the plugin needs to measure options being loaded, it benefits from being loaded itself first. As WordPress loads plugins alphabetically,
Because the plugin needs to measure options being loaded, it benefits from being loaded itself first. As WordPress loads plugins alphabetically,
starting the name with AAA made sense.

### Do I need to take precautions?
Expand All @@ -35,7 +35,7 @@ Yes!! Backup your database.

### How can I add recognized plugins?

Please do a pull request via GitHub on [this file](https://github.com/Emilia-Capital/aaa-option-optimizer/blob/develop/known-plugins/known-plugins.json) in the plugin.
Please do a pull request via GitHub on [this file](https://github.com/ProgressPlanner/aaa-option-optimizer/blob/develop/known-plugins/known-plugins.json) in the plugin.

### How can I report security bugs?

Expand Down
58 changes: 43 additions & 15 deletions aaa-option-optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin that tracks autoloaded options usage and allows the user to optimize them.
*
* @package Emilia\OptionOptimizer
* @package Progress_Planner\OptionOptimizer
*
* Plugin Name: AAA Option Optimizer
* Plugin URI: https://progressplanner.com/plugins/aaa-option-optimizer/
Expand All @@ -27,12 +27,16 @@
register_deactivation_hook( __FILE__, 'aaa_option_optimizer_deactivation' );

/**
* Activation hooked function to store start stats.
* Activation hooked function to store start stats and create table.
*
* @return void
*/
function aaa_option_optimizer_activation() {
global $wpdb;

// Create the custom table.
Progress_Planner\OptionOptimizer\Database::create_table();

$autoload_values = \wp_autoload_values_to_autoload();
$placeholders = implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) );

Expand All @@ -42,19 +46,23 @@ function aaa_option_optimizer_activation() {
);
// phpcs:enable WordPress.DB

update_option(
'option_optimizer',
[
'starting_point_kb' => ( $result->autoload_size / 1024 ),
'starting_point_num' => $result->count,
'starting_point_date' => current_time( 'mysql' ),
'used_options' => [],
'settings' => [
'option_tracking' => 'pre_option',
// Only set starting point if not already set (preserve existing data).
$existing = get_option( 'option_optimizer' );
if ( empty( $existing['starting_point_date'] ) ) {
update_option(
'option_optimizer',
[
'starting_point_kb' => ( $result->autoload_size / 1024 ),
'starting_point_num' => $result->count,
'starting_point_date' => current_time( 'mysql' ),
'used_options' => [], // For backward compatibility.
'settings' => [
'option_tracking' => 'pre_option',
],
],
],
false
);
false
);
}
}

/**
Expand All @@ -67,13 +75,33 @@ function aaa_option_optimizer_deactivation() {
update_option( 'option_optimizer', $aaa_option_value, false );
}

/**
* Ensure database table exists.
* Runs on plugins_loaded to handle existing installs that don't trigger activation.
* Migration is handled via AJAX on the plugin admin page.
*
* @return void
*/
function aaa_option_optimizer_maybe_upgrade() {
// Only run on admin pages, not on AJAX or REST requests to avoid race conditions.
if ( ! is_admin() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
return;
}

// Check if table exists, create if not.
if ( ! Progress_Planner\OptionOptimizer\Database::table_exists() ) {
Progress_Planner\OptionOptimizer\Database::create_table();
}
}
add_action( 'plugins_loaded', 'aaa_option_optimizer_maybe_upgrade' );

/**
* Initializes the plugin.
*
* @return void
*/
function aaa_option_optimizer_init() {
$optimizer = new Emilia\OptionOptimizer\Plugin();
$optimizer = new Progress_Planner\OptionOptimizer\Plugin();
$optimizer->register_hooks();
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "emilia/aaa-option-optimizer",
"name": "progress-planner/aaa-option-optimizer",
"description": "Plugin that tracks autoloaded options usage and allows the user to optimize them.",
"type": "wordpress-plugin",
"license": "GPL-3.0-or-later",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions js/admin-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,74 @@ jQuery( document ).ready( function () {
initializeDataTable( selector );
}
} );

// Migration functionality.
jQuery( '#aaa-start-migration' ).on( 'click', function ( e ) {
e.preventDefault();
const button = jQuery( this );
const progressContainer = jQuery( '#aaa-migration-progress' );
const progressBar = jQuery( '#aaa-migration-progress-bar' );
const statusText = jQuery( '#aaa-migration-status' );
const total = aaaOptionOptimizer.migration.total;

button.prop( 'disabled', true );
progressContainer.show();
statusText.text( aaaOptionOptimizer.i18n.migrating );

/**
* Performs a single migration chunk via AJAX.
*/
function migrateChunk() {
jQuery.ajax( {
url:
aaaOptionOptimizer.root +
'aaa-option-optimizer/v1/migrate',
method: 'POST',
beforeSend: ( xhr ) =>
xhr.setRequestHeader(
'X-WP-Nonce',
aaaOptionOptimizer.nonce
),
success( response ) {
if ( ! response.success ) {
statusText.text(
response.message ||
aaaOptionOptimizer.i18n.migrationError
);
button.prop( 'disabled', false );
return;
}

const migrated = total - response.remaining;
const percent = Math.round( ( migrated / total ) * 100 );

progressBar.css( 'width', percent + '%' );
statusText.text(
aaaOptionOptimizer.i18n.migratedOf
.replace( '%1$d', migrated )
.replace( '%2$d', total )
);

if ( response.remaining > 0 ) {
// Continue with next chunk.
migrateChunk();
} else {
// Migration complete.
statusText.text(
aaaOptionOptimizer.i18n.migrationComplete
);
setTimeout( function () {
window.location.reload();
}, 1000 );
}
},
error() {
statusText.text( aaaOptionOptimizer.i18n.migrationError );
button.prop( 'disabled', false );
},
} );
}

migrateChunk();
} );
} );
3 changes: 2 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="Emilia"/>
<element value="Progress_Planner"/>
<element value="aaa_option_optimizer"/>
<element value="AAA"/>
</property>
</properties>
</rule>
Expand Down
6 changes: 3 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Yes!! Backup your database.

= Where can I report bugs? =

Please use [our GitHub](https://github.com/emilia-Capital/aaa-option-optimizer/) for reporting bugs or making code suggestions. Feel free to use the forums for asking questions too, of course.
Please use [our GitHub](https://github.com/ProgressPlanner/aaa-option-optimizer/) for reporting bugs or making code suggestions. Feel free to use the forums for asking questions too, of course.

For security issues, please see the next question.

Expand All @@ -39,7 +39,7 @@ You can report security bugs through the Patchstack Vulnerability Disclosure Pro

= How can I add recognized plugins? =

Please do a pull request via GitHub on [this file](https://github.com/Emilia-Capital/aaa-option-optimizer/blob/develop/known-plugins/known-plugins.json) in the plugin.
Please do a pull request via GitHub on [this file](https://github.com/ProgressPlanner/aaa-option-optimizer/blob/develop/known-plugins/known-plugins.json) in the plugin.

== Installation ==
1. Search for AAA Option Optimizer on the repository.
Expand Down Expand Up @@ -113,7 +113,7 @@ Implement the missing functionality to create an option with value `false` when
= 1.1 =

The plugin now recognizes plugins from which the options came (thanks to a great pull by [Rogier Lankhorst](https://profiles.wordpress.org/rogierlankhorst/)). If you're a plugin developer and want your plugin's options
properly recognized, please do a pull request [on this file](https://github.com/Emilia-Capital/aaa-option-optimizer/blob/main/known-plugins/known-plugins.json).
properly recognized, please do a pull request [on this file](https://github.com/ProgressPlanner/aaa-option-optimizer/blob/main/known-plugins/known-plugins.json).

Small enhancements:

Expand Down
4 changes: 2 additions & 2 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/**
* Autoload PHP classes for the plugin.
*
* @package Emilia\OptionOptimizer
* @package Progress_Planner\OptionOptimizer
*/

spl_autoload_register(
function ( $class_name ) {
$prefix = 'Emilia\\OptionOptimizer\\';
$prefix = 'Progress_Planner\\OptionOptimizer\\';

if ( 0 !== \strpos( $class_name, $prefix ) ) {
return;
Expand Down
45 changes: 40 additions & 5 deletions src/class-admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
/**
* Admin page functionality for AAA Option Optimizer.
*
* @package Emilia\OptionOptimizer
* @package Progress_Planner\OptionOptimizer
*/

namespace Emilia\OptionOptimizer;
namespace Progress_Planner\OptionOptimizer;

/**
* Admin page functionality for AAA Option Optimizer.
Expand Down Expand Up @@ -189,9 +189,10 @@ public function enqueue_scripts( $hook ) {
'aaa-option-optimizer-admin-js',
'aaaOptionOptimizer',
[
'root' => \esc_url_raw( \rest_url() ),
'nonce' => \wp_create_nonce( 'wp_rest' ),
'i18n' => [
'root' => \esc_url_raw( \rest_url() ),
'nonce' => \wp_create_nonce( 'wp_rest' ),
'migration' => Database::get_migration_status(),
'i18n' => [
'filterBySource' => \esc_html__( 'Filter by source', 'aaa-option-optimizer' ),
'showValue' => \esc_html__( 'Show', 'aaa-option-optimizer' ),
'addAutoload' => \esc_html__( 'Add autoload', 'aaa-option-optimizer' ),
Expand All @@ -207,6 +208,11 @@ public function enqueue_scripts( $hook ) {
'apply' => \esc_html__( 'Apply', 'aaa-option-optimizer' ),

'search' => \esc_html__( 'Search:', 'aaa-option-optimizer' ),
'migrating' => \esc_html__( 'Migrating...', 'aaa-option-optimizer' ),
'migrationComplete' => \esc_html__( 'Migration complete! Reloading page...', 'aaa-option-optimizer' ),
'migrationError' => \esc_html__( 'Migration error. Please try again.', 'aaa-option-optimizer' ),
/* translators: %1$d: number of migrated options, %2$d: total number of options */
'migratedOf' => \esc_html__( 'Migrated %1$d of %2$d options', 'aaa-option-optimizer' ),
'entries' => [
'_' => \esc_html__( 'entries', 'aaa-option-optimizer' ),
'1' => \esc_html__( 'entry', 'aaa-option-optimizer' ),
Expand Down Expand Up @@ -260,10 +266,39 @@ public function render_admin_page_ajax() {
$wpdb->prepare( "SELECT count(*) AS count, SUM( LENGTH( option_value ) ) as autoload_size FROM {$wpdb->options} WHERE autoload IN ( $placeholders )", $autoload_values )
);
// phpcs:enable WordPress.DB

// Check if migration is needed.
$migration_status = Database::get_migration_status();
?>
<div class="wrap">
<h1><?php \esc_html_e( 'AAA Option Optimizer', 'aaa-option-optimizer' ); ?></h1>

<?php if ( $migration_status['needs_migration'] ) : ?>
<div id="aaa-migration-notice" class="notice notice-warning">
<p>
<strong><?php \esc_html_e( 'Data Migration Required', 'aaa-option-optimizer' ); ?></strong><br>
<?php
printf(
/* translators: %d: number of options to migrate */
\esc_html__( 'We need to migrate %d tracked options to the new database format.', 'aaa-option-optimizer' ),
(int) $migration_status['remaining']
);
?>
</p>
<div id="aaa-migration-progress" style="display: none; margin: 10px 0;">
<div style="background: #e0e0e0; border-radius: 4px; height: 20px; width: 100%; max-width: 400px;">
<div id="aaa-migration-progress-bar" style="background: #0073aa; height: 100%; border-radius: 4px; width: 0%; transition: width 0.3s;"></div>
</div>
<p id="aaa-migration-status" style="margin: 5px 0;"></p>
</div>
<p>
<button id="aaa-start-migration" class="button button-primary" type="button">
<?php \esc_html_e( 'Start Migration', 'aaa-option-optimizer' ); ?>
</button>
</p>
</div>
<?php endif; ?>

<p><?php \esc_html_e( 'We\'ve found the following things you can maybe optimize:', 'aaa-option-optimizer' ); ?></p>

<div class="aaa-option-optimizer-tabs">
Expand Down
Loading