Skip to content

Commit

Permalink
Streamlining load sequence 2: decoupled installation and load
Browse files Browse the repository at this point in the history
  • Loading branch information
sybrew committed Jun 22, 2024
1 parent 8b22a2e commit 533833e
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 108 deletions.
5 changes: 3 additions & 2 deletions bootstrap/install.php → bootstrap/install-tsf.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TSF_Extension_Manager;

\defined( 'TSF_EXTENSION_MANAGER_PLUGIN_BASE_FILE' ) or die;
\defined( 'TSF_EXTENSION_MANAGER_PRESENT' ) or die;

/**
* The SEO Framework - Extension Manager plugin
Expand All @@ -24,7 +24,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

\add_action( 'tsfem_needs_the_seo_framework', __NAMESPACE__ . '\\_prepare_tsf_installer' );
\add_action( 'admin_init', __NAMESPACE__ . '\\_prepare_tsf_installer' );

/**
* Prepares scripts for TSF "WP v4.6 Shiny Updates" installation.
*
Expand Down
117 changes: 56 additions & 61 deletions bootstrap/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TSF_Extension_Manager;

\defined( 'TSF_EXTENSION_MANAGER_PLUGIN_BASE_FILE' ) or die;
\defined( 'TSF_EXTENSION_MANAGER_PRESENT' ) or die;

/**
* The SEO Framework - Extension Manager plugin
Expand All @@ -25,30 +25,63 @@
*/

\add_action( 'plugins_loaded', __NAMESPACE__ . '\\_init_locale', 4 );
\add_action( 'plugins_loaded', __NAMESPACE__ . '\\_load_tsfem', 5 );

/**
* Loads plugin locale: 'the-seo-framework-extension-manager'
* Locale folder: the-seo-framework-extension-manager/language/
*
* @hook plugins_loaded 4
* @since 1.0.0
* @access private
*
* @param bool $ignore Whether to load locale outside of the admin area.
* @return void Early if already loaded.
*/
function _init_locale( $ignore = false ) {
if ( \is_admin() || $ignore ) {
if ( \TSF_Extension_Manager\has_run( __METHOD__ ) )
return;

static $has_loaded = false;

if ( ! $has_loaded && ( \is_admin() || $ignore ) ) {

\load_plugin_textdomain(
'the-seo-framework-extension-manager',
false,
\dirname( \TSF_EXTENSION_MANAGER_PLUGIN_BASENAME ) . \DIRECTORY_SEPARATOR . 'language'
\dirname( \TSF_EXTENSION_MANAGER_PLUGIN_BASENAME ) . \DIRECTORY_SEPARATOR . 'language',
);

$has_loaded = true;
}
}

/**
* Loads the plugin.
*
* @hook plugins_loaded 5
* @since 2.7.0
* @access private
* @uses constant \PHP_INT_MIN, available from PHP 7.0
*/
function _load_tsfem() {

if ( ! \function_exists( 'tsf' ) ) {
if ( \is_admin() )
require \TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'install-tsf.php';
return;
}

// Prepare plugin upgrader before the plugin loads.
if ( \tsf_extension_manager_db_version() < \TSF_EXTENSION_MANAGER_DB_VERSION )
require \TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'upgrade.php';

\TSF_Extension_Manager\_protect_options();
\TSF_Extension_Manager\_register_autoloader();

_init_tsf_extension_manager();
// TODO var_Dump() next pass:
// \add_action( 'the_seo_framework_loaded', __NAMESPACE__ . '\\_init_tsf_extension_manager', 0 );
}

\TSF_Extension_Manager\_protect_options();
/**
* Prevents option handling outside of the plugin's scope.
* Warning: When you remove these filters or actions, the plugin will delete all its options on first sight.
Expand Down Expand Up @@ -112,7 +145,6 @@ function _pre_execute_protect_option( $new_value, $old_value, $option ) {
return SecureOption::verify_option_instance( $new_value, $old_value, $option );
}

\add_action( 'plugins_loaded', __NAMESPACE__ . '\\_init_tsf_extension_manager', 6 );
/**
* Loads TSF_Extension_Manager\LoadAdmin class when in admin.
* Loads TSF_Extension_Manager\LoadFront class on the front-end.
Expand All @@ -127,7 +159,9 @@ function _pre_execute_protect_option( $new_value, $old_value, $option ) {
*
* Performs wp_die() when called prior to action `plugins_loaded`.
*
* @hook the_seo_framework_loaded 0
* @since 1.0.0
* @since 2.7.0 Moved from plugins_loaded 6 to the_seo_framework_loaded (init 0).
* @access private
* @factory
*
Expand All @@ -141,12 +175,22 @@ function _init_tsf_extension_manager() {
if ( $tsfem )
return $tsfem;

if ( ! \doing_action( 'plugins_loaded' ) ) {
\wp_die( 'Use tsfem() after action `plugins_loaded` priority 6.' );
exit;
}
if ( version_compare( \THE_SEO_FRAMEWORK_VERSION, '4.2.8', '<' ) )
return null;

/**
* @since 1.0.0
* @param bool $can_load
*/
if ( \apply_filters( 'tsf_extension_manager_enabled', true ) ) {
/**
* @since 2.6.3
* @internal
*/
\define( 'TSF_EXTENSION_MANAGER_USE_MODERN_TSF', version_compare( \THE_SEO_FRAMEWORK_VERSION, '4.3.0', '>=' ) );

if ( \TSF_Extension_Manager\can_load_class() ) {
// Load TSF v5.0 transition functions file.
require TSF_EXTENSION_MANAGER_DIR_PATH_FUNCTION . 'transition.php';

/**
* Load class overloading traits.
Expand All @@ -171,19 +215,11 @@ function _init_tsf_extension_manager() {
* @since 1.5.0
*/
\do_action( 'tsfem_extensions_initialized' );
} elseif ( ! \function_exists( 'tsf' ) ) {
/**
* Nothing is loaded at this point; not even The SEO Framework.
*
* @since 2.2.0
*/
\do_action( 'tsfem_needs_the_seo_framework' );
}

return $tsfem;
}

\TSF_Extension_Manager\_register_autoloader();
/**
* Registers The SEO Framework extension manager's autoloader.
*
Expand Down Expand Up @@ -213,47 +249,6 @@ class_exists( $_class, false ) and die;
spl_autoload_register( __NAMESPACE__ . '\\_autoload_classes', true, true );
}

/**
* Determines whether we can load the the plugin.
* Memoizes the result.
*
* @since 1.0.0
* @since 1.5.0 Now requires TSF 2.8+ to load.
* @since 2.0.2 Now requires TSF 3.1+ to load.
* @since 2.2.0 Now requires TSF 3.3+ to load.
* @since 2.5.0 Now requires TSF 4.1.2+ to load.
* @since 2.5.1 Now requires TSF 4.1.4+ to load.
* @since 2.6.0 Now requires TSF 4.2.0+ to load.
* @since 2.6.2 Now requires TSF 4.2.8+ to load.
*
* @return bool Whether the plugin can load. Always returns false on the front-end.
*/
function can_load_class() {

static $can_load;

if ( isset( $can_load ) )
return $can_load;

if ( \did_action( 'the_seo_framework_loaded' ) ) {
/**
* @since 2.6.3
* @internal
*/
\define( 'TSF_EXTENSION_MANAGER_USE_MODERN_TSF', version_compare( \THE_SEO_FRAMEWORK_VERSION, '4.3.0', '>=' ) );

if ( version_compare( \THE_SEO_FRAMEWORK_VERSION, '4.2.8', '>=' ) ) {
/**
* @since 1.0.0
* @param bool $can_load
*/
return $can_load = (bool) \apply_filters( 'tsf_extension_manager_enabled', true );
}
}

return false;
}

/**
* Autoloads all class files. To be used when requiring access to all or any of
* the plugin classes.
Expand Down
56 changes: 31 additions & 25 deletions bootstrap/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@

namespace TSF_Extension_Manager;

\defined( 'TSF_EXTENSION_MANAGER_PLUGIN_BASE_FILE' ) or die;

use function \TSF_Extension_Manager\Transition\{
convert_markdown,
do_dismissible_notice,
};
\defined( 'TSF_EXTENSION_MANAGER_PRESENT' ) or die;

/**
* The SEO Framework - Extension Manager plugin
Expand All @@ -30,13 +25,18 @@
*/

\add_action( 'admin_notices', __NAMESPACE__ . '\\_check_external_blocking' );
\add_filter( 'plugins_api', __NAMESPACE__ . '\\_hook_plugins_api', \PHP_INT_MAX, 3 );
\add_action( 'upgrader_process_complete', __NAMESPACE__ . '\\_clear_update_cache' );
\add_filter( 'pre_set_site_transient_update_plugins', __NAMESPACE__ . '\\_push_update', \PHP_INT_MAX, 2 );

/**
* Checks whether the WP installation blocks external requests.
* Shows notice if external requests are blocked through the WP_HTTP_BLOCK_EXTERNAL constant
*
* If you must, you can disable this notice by implementing this snippet:
* `remove_action( 'admin_notices', 'TSF_Extension_Manager\\_check_external_blocking' );`
*
* @hook admin_notices 10
* @since 2.0.0
* @access private
*/
Expand All @@ -54,29 +54,37 @@ function _check_external_blocking() {
// We rely on TSF here but it might not be available. Still, not outputting this notice does not harm.
if ( ! \function_exists( 'tsf' ) ) return;

$notice = convert_markdown(
sprintf(
/* translators: Markdown. %s = Update API URL */
\esc_html__(
'This website is blocking external requests, this means it will not be able to connect to The SEO Framework update services. Please add `%s` to `WP_ACCESSIBLE_HOSTS` to keep the Extension Manager plugin up-to-date and secure.',
'the-seo-framework-extension-manager'
),
\esc_html( $host )
$notice = sprintf(
/* translators: Markdown. %s = Update API URL */
\esc_html__(
'This website is blocking external requests, this means it will not be able to connect to The SEO Framework update services. Please add `%s` to `WP_ACCESSIBLE_HOSTS` to keep the Extension Manager plugin up-to-date and secure.',
'the-seo-framework-extension-manager'
),
[ 'code' ]
);
do_dismissible_notice(
$notice,
[
'type' => 'error',
'escape' => false,
]
\esc_html( $host ),
);

// See tsf()->markdown()'s code() (private func).
preg_match_all( '/`([^`]+)`/', $notice, $matches, \PREG_SET_ORDER );

foreach ( $matches as $match ) {
$notice = str_replace(
$match[0],
sprintf( '<code>%s</code>', \esc_html( $match[1] ) ),
$notice,
);
}

// TODO consider using wp_admin_notice() (WP 6.4+)
// phpcs:ignore, WordPress.Security.EscapeOutput.OutputNotEscaped -- $notice is escaped.
echo <<<HTML
<div class="notice notice-warning is-dismissible">
<p>$notice</p>
</div>
HTML;
}
}
}

\add_filter( 'plugins_api', __NAMESPACE__ . '\\_hook_plugins_api', \PHP_INT_MAX, 3 );
/**
* Filters the plugin API to bind to The SEO Framework's own updater service.
*
Expand Down Expand Up @@ -155,7 +163,6 @@ function _hook_plugins_api( $res, $action, $args ) {
return $res;
}

\add_action( 'upgrader_process_complete', __NAMESPACE__ . '\\_clear_update_cache' );
/**
* Clears the updater cache after a plugin's been updated.
* This prevents incorrect updater version storing.
Expand All @@ -168,7 +175,6 @@ function _clear_update_cache() {
\update_site_option( \TSF_EXTENSION_MANAGER_UPDATER_CACHE, [] );
}

\add_filter( 'pre_set_site_transient_update_plugins', __NAMESPACE__ . '\\_push_update', \PHP_INT_MAX, 2 );
/**
* Push values into the update_plugins site transient.
* This allows for multisite network updates.
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TSF_Extension_Manager;

\defined( 'TSF_EXTENSION_MANAGER_DB_VERSION' ) or die;
\defined( 'TSF_EXTENSION_MANAGER_PRESENT' ) or die;

/**
* The SEO Framework - Extension Manager plugin
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/upgrader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace TSF_Extension_Manager;

\defined( 'TSF_EXTENSION_MANAGER_DB_VERSION' ) or die;
\defined( 'TSF_EXTENSION_MANAGER_PRESENT' ) or die;

/**
* The SEO Framework - Extension Manager plugin
Expand Down
4 changes: 1 addition & 3 deletions inc/functions/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ function _load_wp_compat( $version = '' ) {
// include an unmodified $wp_version
include \ABSPATH . \WPINC . '/version.php';

/**
* @global string $wp_version
*/
// phpcs:ignore, VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- $wp_version is included.
if ( version_compare( $wp_version, $version, '>=' ) )
return $loaded[ $version ] = true;

Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ TODO myMart and myMart Pharmacy isn't clear, they should be separated.
-> Remove the brackets in `{store name} {department name}`,
-> and make it `store name` OR `store name department name`

TODO trailing commas

TODO we can use is_wp_version_compatible() and is_php_version_compatible() now.

= 2.7.0 =

* June TODOth, 2024
Expand Down
21 changes: 6 additions & 15 deletions the-seo-framework-extension-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: The SEO Framework - Extension Manager
* Plugin URI: https://theseoframework.com/extension-manager/
* Description: Add more powerful SEO features to The SEO Framework. Right from your WordPress dashboard.
* Version: 2.7.0-dev-31
* Version: 2.7.0-dev-32
* Author: The SEO Framework Team
* Author URI: https://theseoframework.com/
* License: GPLv3
Expand Down Expand Up @@ -72,23 +72,14 @@
dirname( TSF_EXTENSION_MANAGER_PLUGIN_BASE_FILE ) . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR
);

// Defines environental constants.
// Define environental constants.
require TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'define.php';

// Load plugin API file.
// Load plugin API functions.
require TSF_EXTENSION_MANAGER_DIR_PATH_FUNCTION . 'api.php';

// Load TSF v5.0 transition functions file.
require TSF_EXTENSION_MANAGER_DIR_PATH_FUNCTION . 'transition.php';
// Load plugin updater.
require \TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'update.php';

// Prepare plugin upgrader before the plugin loads.
if ( tsf_extension_manager_db_version() < TSF_EXTENSION_MANAGER_DB_VERSION )
require TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'upgrade.php';

require TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'update.php';

if ( is_admin() )
require TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'install.php';

// Load plugin files.
// Load plugin.
require TSF_EXTENSION_MANAGER_BOOTSTRAP_PATH . 'load.php';

0 comments on commit 533833e

Please sign in to comment.