From 1fd70179daaf227715f0fb60f9a57e139a1cb43a Mon Sep 17 00:00:00 2001 From: JiveDig Date: Mon, 8 Jan 2024 15:48:32 -0500 Subject: [PATCH 1/2] move more functions to main init file --- lib/functions/utilities.php | 140 ------ lib/init.php | 872 ++++++++++++++++++++---------------- 2 files changed, 475 insertions(+), 537 deletions(-) diff --git a/lib/functions/utilities.php b/lib/functions/utilities.php index 81888cfcd..9194100ca 100644 --- a/lib/functions/utilities.php +++ b/lib/functions/utilities.php @@ -12,146 +12,6 @@ // Prevent direct file access. defined( 'ABSPATH' ) || die; -/** - * Returns asset version with filetime. - * - * @since 0.1.0 - * - * @param string $file File path. - * - * @return string - */ -function mai_get_asset_version( $file ) { - $file = str_replace( content_url(), WP_CONTENT_DIR, $file ); - $version = mai_get_version(); - - if ( file_exists( $file ) ) { - $version .= '.' . date( 'njYHi', filemtime( $file ) ); - } - - return $version; -} - -/** - * Returns minified version of asset if in dev mode. - * - * @since 0.1.0 - * @since 2.4.0 Removed min dir if CSS file. Always return minified CSS. - * - * @param string $file File base name (relative to type directory). - * - * @return string - */ -function mai_get_asset_url( $file ) { - $type = false !== strpos( $file, '.js' ) ? 'js' : 'css'; - $name = str_replace( [ '.js', '.css' ], '', $file ); - $uri = mai_get_url(); - $default = "${uri}assets/${type}/${name}.${type}"; - $dir = 'js' === $type ? '/min/' : '/'; - $min = "${uri}assets/${type}${dir}${name}.min.${type}"; - - return mai_is_in_dev_mode() && 'js' === $type ? $default : $min; -} - -/** - * Returns the active child theme's sub config. - * - * @since 0.1.0 - * @since 2.11.0 Add static caching. - * - * @param string $sub_config Name of config to get. - * - * @return array - */ -function mai_get_config( $sub_config ) { - static $configs = null; - - if ( is_array( $configs ) && isset( $configs[ $sub_config ] ) ) { - return $configs[ $sub_config ]; - } - - if ( ! is_array( $configs ) ) { - $configs = []; - } - - $config = mai_get_full_config(); - $value = isset( $config[ $sub_config ] ) ? $config[ $sub_config ] : []; - $configs[ $sub_config ] = apply_filters( "mai_{$sub_config}_config", $value ); - - return $configs[ $sub_config ]; -} - -/** - * Returns the active child theme's full config. - * - * @access private - * - * @since 2.11.0 - * - * @return array - */ -function mai_get_full_config() { - static $config = null; - - if ( ! is_null( $config ) ) { - return $config; - } - - $config = require mai_get_dir() . 'config/_default.php'; - $theme = mai_get_active_theme(); - $theme = ( 'default' === $theme ) ? '_default' : $theme; - $path = mai_get_dir() . 'config/' . $theme . '.php'; - - if ( is_readable( $path ) ) { - $new = require $path; - $config = array_replace_recursive( $config, $new ); - if ( isset( $new['settings']['content-archives'] ) ) { - foreach ( $new['settings']['content-archives'] as $key => $settings ) { - if ( ! ( isset( $new['settings']['content-archives'][ $key ]['show'] ) && isset( $config['settings']['content-archives'][ $key ]['show'] ) ) ) { - continue; - } - $config['settings']['content-archives'][ $key ]['show'] = $new['settings']['content-archives'][ $key ]['show']; - } - } - if ( isset( $new['settings']['single-content'] ) ) { - foreach ( $new['settings']['single-content'] as $key => $settings ) { - if ( ! ( isset( $new['settings']['single-content'][ $key ]['show'] ) && isset( $config['settings']['single-content'][ $key ]['show'] ) ) ) { - continue; - } - $config['settings']['single-content'][ $key ]['show'] = $new['settings']['single-content'][ $key ]['show']; - } - } - } - - // Allow users to override from within actual child theme. - $child = get_stylesheet_directory() . '/config.php'; - - if ( is_readable( $child ) ) { - $new = require $child; - $config = array_replace_recursive( $config, $new ); - if ( isset( $new['settings']['content-archives'] ) ) { - foreach ( $new['settings']['content-archives'] as $key => $settings ) { - if ( ! ( isset( $new['settings']['content-archives'][ $key ]['show'] ) && isset( $config['settings']['content-archives'][ $key ]['show'] ) ) ) { - continue; - } - $config['settings']['content-archives'][ $key ]['show'] = $new['settings']['content-archives'][ $key ]['show']; - } - } - if ( isset( $new['settings']['single-content'] ) ) { - foreach ( $new['settings']['single-content'] as $key => $settings ) { - if ( ! ( isset( $new['settings']['single-content'][ $key ]['show'] ) && isset( $config['settings']['single-content'][ $key ]['show'] ) ) ) { - continue; - } - $config['settings']['single-content'][ $key ]['show'] = $new['settings']['single-content'][ $key ]['show']; - } - } - } - - $config = apply_filters( 'mai_config', $config ); - - return $config; -} - /** * Adds slug to plugin data array. * Slug is used as the key so mai_get_config can merge default diff --git a/lib/init.php b/lib/init.php index eeff5e3d3..46fb45b24 100644 --- a/lib/init.php +++ b/lib/init.php @@ -13,464 +13,148 @@ defined( 'ABSPATH' ) || die(); /** - * Get the active engine theme. Defaults to the default theme. + * Deactivate Mai Engine plugin. * * @since 0.1.0 * - * @return bool|string + * @return void */ -function mai_get_engine_theme() { - static $theme = null; - - if ( ! is_null( $theme ) ) { - return $theme; - } - - $engine_themes = mai_get_engine_themes(); - - if ( current_theme_supports( 'mai-engine' ) ) { - // Custom themes can load a specific theme default via `add_theme_support( 'mai-engine', 'success' );`. - $theme_support = get_theme_support( 'mai-engine' ); - - if ( $theme_support && is_array( $theme_support ) && in_array( $theme_support[0], $engine_themes, true ) ) { - $theme = $theme_support[0]; - } - } - - if ( ! $theme ) { - $current_theme = defined( 'CHILD_THEME_NAME' ) ? CHILD_THEME_NAME : null; - - if ( $current_theme ) { - $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); - - if ( in_array( $current_theme, $engine_themes, true ) ) { - $theme = $current_theme; - } - } - } - - if ( ! $theme ) { - $current_theme = wp_get_theme()->get( 'Name' ); - - if ( $current_theme ) { - $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); - - if ( in_array( $current_theme, $engine_themes, true ) ) { - $theme = $current_theme; - } - } - } - - if ( ! $theme ) { - $current_theme = wp_get_theme()->get( 'TextDomain' ); - - if ( $current_theme ) { - $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); - - if ( in_array( $current_theme, $engine_themes, true ) ) { - $theme = $current_theme; - } - } - } - - if ( ! $theme ) { - $current_theme = basename( get_stylesheet_directory() ); - - if ( $current_theme ) { - $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); - - if ( in_array( $current_theme, $engine_themes, true ) ) { - $theme = $current_theme; - } - } - } - - $theme = $theme ?: 'default'; - - return $theme; +function mai_deactivate_plugin() { + deactivate_plugins( basename( dirname( __DIR__ ) ) . '/mai-engine.php' ); } /** - * Get available engine themes. + * Show notice that Mai Engine has been deactivated. * - * @since 2.0.0 + * @since 0.1.0 * - * @return array + * @return void */ -function mai_get_engine_themes() { - static $themes = null; - - if ( ! is_null( $themes ) ) { - return $themes; - } - - $configs = glob( dirname( __DIR__ ) . '/config/*.php' ); - $themes = []; - - foreach ( $configs as $index => $config ) { - $base = basename( $config, '.php' ); - - if ( in_array( $base, [ '_default', '_settings' ], true ) ) { - continue; - } +function mai_deactivate_plugin_notice() { + printf( + '

%s%s%s%s%s

', + __( 'Your theme does not support the ', 'mai-engine' ), + 'Mai Engine', + __( ' plugin. As a result, ', 'mai-engine' ), + 'Mai Engine', + __( ' has been deactivated.', 'mai-engine' ) + ); - $themes[] = $base; + if ( isset( $_GET['activate'] ) ) { + unset( $_GET['activate'] ); } - - return $themes; } +add_action( 'setup_theme', 'mai_load_genesis', 100 ); /** - * Returns the plugin directory. + * Starts the engine. + * + * Enables the use of `genesis_*` functions in the child theme functions.php file, + * without the need for require_once get_template_directory() . '/lib/init.php'. + * This allows us to provide a truly blank child theme for users to work with. * * @since 0.1.0 * - * @return string + * @return void */ -function mai_get_dir() { - static $dir = null; +function mai_load_genesis() { + $init = get_template_directory() . '/lib/init.php'; - if ( is_null( $dir ) ) { - $dir = trailingslashit( dirname( dirname( __DIR__ ) ) ); + if ( is_readable( $init ) ) { + require_once $init; } - - return $dir; } /** - * Returns the plugin URL. + * Removes deprecated stuff from Genesis. * - * @since 0.1.0 + * @since 2.22.0 * - * @return string + * @return bool */ -function mai_get_url() { - static $url = null; - - if ( ! is_null( $url ) ) { - return $url; - } - - $url = trailingslashit( plugins_url( basename( mai_get_dir() ) ) ); - - return $url; -} +add_filter( 'genesis_load_deprecated', '__return_false' ); +add_action( 'genesis_init', 'mai_modify_genesis_defaults', 5 ); /** - * Gets the plugin basename. + * Removes all Genesis functions that use the is_child_theme() function. + * + * Since we are loading Genesis on behalf of the child theme, functions won't + * work correctly. This workaround will fix the issue by removing functions + * that contain the check and adds theme support that is required early. + * + * Also adds breadcrumb support back before genesis init. * * @since 0.1.0 * - * @return string + * @return void */ -function mai_get_base() { - static $base = null; - - if ( ! is_null( $base ) ) { - return $base; - } - - $dir = basename( dirname( dirname( __DIR__ ) ) ); - $file = mai_get_handle() . '.php'; - $base = $dir . DIRECTORY_SEPARATOR . $file; - - return $base; +function mai_modify_genesis_defaults() { + remove_action( 'genesis_init', 'genesis_theme_support' ); + add_filter( 'genesis_initial_layouts', '__return_empty_array' ); + add_theme_support( 'genesis-breadcrumbs' ); // TODO: This is in config already right? } +add_action( 'genesis_setup', 'mai_remove_genesis_default_widget_areas', 8 ); /** - * Returns an array of plugin data from the main plugin file. + * Removes Genesis default widget areas. * - * @since 0.1.0 + * We'll re-register them via our config. * - * @param string $key Optionally return one key. + * @since 0.3.7 * - * @return array|string|null + * @return void */ -function mai_get_plugin_data( $key = '' ) { - static $data = null; - - if ( is_null( $data ) ) { - $data = get_file_data( - mai_get_dir() . 'mai-engine.php', - [ - 'name' => 'Plugin Name', - 'version' => 'Version', - 'plugin-uri' => 'Plugin URI', - 'text-domain' => 'Text Domain', - 'description' => 'Description', - 'author' => 'Author', - 'author-uri' => 'Author URI', - 'domain-path' => 'Domain Path', - 'network' => 'Network', - ], - 'plugin' - ); - } - - if ( array_key_exists( $key, $data ) ) { - return $data[ $key ]; - } - - return $data; +function mai_remove_genesis_default_widget_areas() { + remove_action( 'genesis_setup', 'genesis_register_default_widget_areas' ); } +add_filter( 'genesis_theme_settings_menu_ops', 'mai_genesis_theme_settings_menu_ops' ); /** - * Returns the plugin name. + * Makes room for our Mai Theme admin menu. + * These only worked when the value was a string. * - * @since 0.1.0 + * @since 2.10.0 * - * @return string + * @param array $options The existing options from Genesis. + * + * @return array */ -function mai_get_name() { - static $name = null; +function mai_genesis_theme_settings_menu_ops( $options ) { + if ( isset( $options['main_menu']['sep']['sep_position'] ) ) { + $options['main_menu']['sep']['sep_position'] = '58.994'; + } - if ( is_null( $name ) ) { - $name = mai_get_plugin_data( 'name' ); + if ( isset( $options['main_menu']['position'] ) ) { + $options['main_menu']['position'] = '58.996'; } - return $name; + return $options; } /** - * Returns the plugin handle/text domain. + * Load default favicon. * - * @since 0.1.0 + * @since 2.4.3 + * @since 2.6.0 Changed function name to avoid clash when switching from v1 to v2. + * @since 2.6.0 Check mai_get_url() function exists. We saw this run too early and fail. + * @since 2.18.0 Added `get_site_icon_url` filter because it was throwing errors in admin for missing favicon. + * @link https://github.com/maithemewp/mai-engine/issues/361 * * @return string */ -function mai_get_handle() { - static $handle = null; - - if ( is_null( $handle ) ) { - $handle = mai_get_plugin_data( 'text-domain' ); +add_filter( 'get_site_icon_url', 'mai_load_default_favicon' ); +add_filter( 'genesis_pre_load_favicon', 'mai_load_default_favicon' ); +function mai_load_default_favicon( $favicon ) { + if ( ! $favicon && function_exists( 'mai_get_url' ) ) { + return mai_get_url() . 'assets/img/icon-256x256.png'; } - return $handle; + return $favicon; } /** - * Returns the plugin version. - * - * @since 0.1.0 - * - * @return string - */ -function mai_get_version() { - static $version = null; - - if ( is_null( $version ) ) { - $version = mai_get_plugin_data( 'version' ); - } - - return $version; -} - -/** - * Gets the plugin updater icons. - * This may be used in additiona Mai Plugins. - * - * @since 2.11.0 - * - * @return array - */ -function mai_get_updater_icons() { - $icons = []; - $standard = mai_get_logo_icon_1x(); - $retina = mai_get_logo_icon_2x(); - - if ( $standard && $retina ) { - $icons = [ - '1x' => $standard, - '2x' => $retina, - ]; - } - - return $icons; -} - -/** - * Gets the Mai Theme logo icon @1x for plugin updater. - * - * @since 2.11.0 - * - * @return string - */ -function mai_get_logo_icon_1x() { - static $icon = null; - - if ( ! is_null( $icon ) ) { - return $icon; - } - - $file = 'assets/img/icon-128x128.png'; - $icon = file_exists( mai_get_dir() . $file ) ? mai_get_url() . $file : ''; - - return $icon; -} - -/** - * Gets the Mai Theme logo icon @1x for plugin updater. - * - * @since 2.11.0 - * - * @return string - */ -function mai_get_logo_icon_2x() { - static $icon = null; - - if ( ! is_null( $icon ) ) { - return $icon; - } - - $file = 'assets/img/icon-256x256.png'; - $icon = file_exists( mai_get_dir() . $file ) ? mai_get_url() . $file : ''; - - return $icon; -} - -/** - * Deactivate Mai Engine plugin. - * - * @since 0.1.0 - * - * @return void - */ -function mai_deactivate_plugin() { - deactivate_plugins( basename( dirname( __DIR__ ) ) . '/mai-engine.php' ); -} - -/** - * Show notice that Mai Engine has been deactivated. - * - * @since 0.1.0 - * - * @return void - */ -function mai_deactivate_plugin_notice() { - printf( - '

%s%s%s%s%s

', - __( 'Your theme does not support the ', 'mai-engine' ), - 'Mai Engine', - __( ' plugin. As a result, ', 'mai-engine' ), - 'Mai Engine', - __( ' has been deactivated.', 'mai-engine' ) - ); - - if ( isset( $_GET['activate'] ) ) { - unset( $_GET['activate'] ); - } -} - -add_action( 'setup_theme', 'mai_load_genesis', 100 ); -/** - * Starts the engine. - * - * Enables the use of `genesis_*` functions in the child theme functions.php file, - * without the need for require_once get_template_directory() . '/lib/init.php'. - * This allows us to provide a truly blank child theme for users to work with. - * - * @since 0.1.0 - * - * @return void - */ -function mai_load_genesis() { - $init = get_template_directory() . '/lib/init.php'; - - if ( is_readable( $init ) ) { - require_once $init; - } -} - -/** - * Removes deprecated stuff from Genesis. - * - * @since 2.22.0 - * - * @return bool - */ -add_filter( 'genesis_load_deprecated', '__return_false' ); - -add_action( 'genesis_init', 'mai_modify_genesis_defaults', 5 ); -/** - * Removes all Genesis functions that use the is_child_theme() function. - * - * Since we are loading Genesis on behalf of the child theme, functions won't - * work correctly. This workaround will fix the issue by removing functions - * that contain the check and adds theme support that is required early. - * - * Also adds breadcrumb support back before genesis init. - * - * @since 0.1.0 - * - * @return void - */ -function mai_modify_genesis_defaults() { - remove_action( 'genesis_init', 'genesis_theme_support' ); - add_filter( 'genesis_initial_layouts', '__return_empty_array' ); - add_theme_support( 'genesis-breadcrumbs' ); // TODO: This is in config already right? -} - -add_action( 'genesis_setup', 'mai_remove_genesis_default_widget_areas', 8 ); -/** - * Removes Genesis default widget areas. - * - * We'll re-register them via our config. - * - * @since 0.3.7 - * - * @return void - */ -function mai_remove_genesis_default_widget_areas() { - remove_action( 'genesis_setup', 'genesis_register_default_widget_areas' ); -} - -add_filter( 'genesis_theme_settings_menu_ops', 'mai_genesis_theme_settings_menu_ops' ); -/** - * Makes room for our Mai Theme admin menu. - * These only worked when the value was a string. - * - * @since 2.10.0 - * - * @param array $options The existing options from Genesis. - * - * @return array - */ -function mai_genesis_theme_settings_menu_ops( $options ) { - if ( isset( $options['main_menu']['sep']['sep_position'] ) ) { - $options['main_menu']['sep']['sep_position'] = '58.994'; - } - - if ( isset( $options['main_menu']['position'] ) ) { - $options['main_menu']['position'] = '58.996'; - } - - return $options; -} - -/** - * Load default favicon. - * - * @since 2.4.3 - * @since 2.6.0 Changed function name to avoid clash when switching from v1 to v2. - * @since 2.6.0 Check mai_get_url() function exists. We saw this run too early and fail. - * @since 2.18.0 Added `get_site_icon_url` filter because it was throwing errors in admin for missing favicon. - * @link https://github.com/maithemewp/mai-engine/issues/361 - * - * @return string - */ -add_filter( 'get_site_icon_url', 'mai_load_default_favicon' ); -add_filter( 'genesis_pre_load_favicon', 'mai_load_default_favicon' ); -function mai_load_default_favicon( $favicon ) { - if ( ! $favicon && function_exists( 'mai_get_url' ) ) { - return mai_get_url() . 'assets/img/icon-256x256.png'; - } - - return $favicon; -} - -/** - * Clears the transient on post type save/update. - * This was running too late in mai_load_files(). + * Clears the transient on post type save/update. + * This was running too late in mai_load_files(). * * @since 2.11.0 * @@ -773,6 +457,400 @@ function mai_load_files() { } } +/** + * Returns the plugin directory. + * + * @since 0.1.0 + * + * @return string + */ +function mai_get_dir() { + static $dir = null; + + if ( is_null( $dir ) ) { + $dir = trailingslashit( dirname( __DIR__ ) ); + } + + return $dir; +} + +/** + * Returns the plugin URL. + * + * @since 0.1.0 + * + * @return string + */ +function mai_get_url() { + static $url = null; + + if ( ! is_null( $url ) ) { + return $url; + } + + $url = trailingslashit( plugins_url( basename( mai_get_dir() ) ) ); + + return $url; +} + +/** + * Gets the plugin basename. + * + * @since 0.1.0 + * + * @return string + */ +function mai_get_base() { + static $base = null; + + if ( ! is_null( $base ) ) { + return $base; + } + + $dir = basename( dirname( dirname( __DIR__ ) ) ); + $file = mai_get_handle() . '.php'; + $base = $dir . DIRECTORY_SEPARATOR . $file; + + return $base; +} + +/** + * Returns an array of plugin data from the main plugin file. + * + * @since 0.1.0 + * + * @param string $key Optionally return one key. + * + * @return array|string|null + */ +function mai_get_plugin_data( $key = '' ) { + static $data = null; + + if ( is_null( $data ) ) { + $data = get_file_data( + mai_get_dir() . 'mai-engine.php', + [ + 'name' => 'Plugin Name', + 'version' => 'Version', + 'plugin-uri' => 'Plugin URI', + 'text-domain' => 'Text Domain', + 'description' => 'Description', + 'author' => 'Author', + 'author-uri' => 'Author URI', + 'domain-path' => 'Domain Path', + 'network' => 'Network', + ], + 'plugin' + ); + } + + if ( array_key_exists( $key, $data ) ) { + return $data[ $key ]; + } + + return $data; +} + +/** + * Returns the plugin name. + * + * @since 0.1.0 + * + * @return string + */ +function mai_get_name() { + static $name = null; + + if ( is_null( $name ) ) { + $name = mai_get_plugin_data( 'name' ); + } + + return $name; +} + +/** + * Returns the plugin handle/text domain. + * + * @since 0.1.0 + * + * @return string + */ +function mai_get_handle() { + static $handle = null; + + if ( is_null( $handle ) ) { + $handle = mai_get_plugin_data( 'text-domain' ); + } + + return $handle; +} + +/** + * Returns the plugin version. + * + * @since 0.1.0 + * + * @return string + */ +function mai_get_version() { + static $version = null; + + if ( is_null( $version ) ) { + $version = mai_get_plugin_data( 'version' ); + } + + return $version; +} + +/** + * Get the active engine theme. Defaults to the default theme. + * + * @since 0.1.0 + * + * @return bool|string + */ +function mai_get_engine_theme() { + static $theme = null; + + if ( ! is_null( $theme ) ) { + return $theme; + } + + $engine_themes = mai_get_engine_themes(); + + if ( current_theme_supports( 'mai-engine' ) ) { + // Custom themes can load a specific theme default via `add_theme_support( 'mai-engine', 'success' );`. + $theme_support = get_theme_support( 'mai-engine' ); + + if ( $theme_support && is_array( $theme_support ) && in_array( $theme_support[0], $engine_themes, true ) ) { + $theme = $theme_support[0]; + } + } + + if ( ! $theme ) { + $current_theme = defined( 'CHILD_THEME_NAME' ) ? CHILD_THEME_NAME : null; + + if ( $current_theme ) { + $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); + + if ( in_array( $current_theme, $engine_themes, true ) ) { + $theme = $current_theme; + } + } + } + + if ( ! $theme ) { + $current_theme = wp_get_theme()->get( 'Name' ); + + if ( $current_theme ) { + $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); + + if ( in_array( $current_theme, $engine_themes, true ) ) { + $theme = $current_theme; + } + } + } + + if ( ! $theme ) { + $current_theme = wp_get_theme()->get( 'TextDomain' ); + + if ( $current_theme ) { + $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); + + if ( in_array( $current_theme, $engine_themes, true ) ) { + $theme = $current_theme; + } + } + } + + if ( ! $theme ) { + $current_theme = basename( get_stylesheet_directory() ); + + if ( $current_theme ) { + $current_theme = str_replace( 'mai-', '', sanitize_title_with_dashes( $current_theme ) ); + + if ( in_array( $current_theme, $engine_themes, true ) ) { + $theme = $current_theme; + } + } + } + + $theme = $theme ?: 'default'; + + return $theme; +} + +/** + * Get available engine themes. + * + * @since 2.0.0 + * + * @return array + */ +function mai_get_engine_themes() { + static $themes = null; + + if ( ! is_null( $themes ) ) { + return $themes; + } + + $configs = glob( dirname( __DIR__ ) . '/config/*.php' ); + $themes = []; + + foreach ( $configs as $index => $config ) { + $base = basename( $config, '.php' ); + + if ( in_array( $base, [ '_default', '_settings' ], true ) ) { + continue; + } + + $themes[] = $base; + } + + return $themes; +} + + +/** + * Returns asset version with filetime. + * + * @since 0.1.0 + * + * @param string $file File path. + * + * @return string + */ +function mai_get_asset_version( $file ) { + $file = str_replace( content_url(), WP_CONTENT_DIR, $file ); + $version = mai_get_version(); + + if ( file_exists( $file ) ) { + $version .= '.' . date( 'njYHi', filemtime( $file ) ); + } + + return $version; +} + +/** + * Returns minified version of asset if in dev mode. + * + * @since 0.1.0 + * @since 2.4.0 Removed min dir if CSS file. Always return minified CSS. + * + * @param string $file File base name (relative to type directory). + * + * @return string + */ +function mai_get_asset_url( $file ) { + $type = false !== strpos( $file, '.js' ) ? 'js' : 'css'; + $name = str_replace( [ '.js', '.css' ], '', $file ); + $uri = mai_get_url(); + $default = "{$uri}assets/{$type}/{$name}.{$type}"; + $dir = 'js' === $type ? '/min/' : '/'; + $min = "{$uri}assets/{$type}{$dir}{$name}.min.{$type}"; + + return mai_is_in_dev_mode() && 'js' === $type ? $default : $min; +} + +/** + * Returns the active child theme's sub config. + * + * @since 0.1.0 + * @since 2.11.0 Add static caching. + * + * @param string $sub_config Name of config to get. + * + * @return array + */ +function mai_get_config( $sub_config ) { + static $configs = null; + + if ( is_array( $configs ) && isset( $configs[ $sub_config ] ) ) { + return $configs[ $sub_config ]; + } + + if ( ! is_array( $configs ) ) { + $configs = []; + } + + $config = mai_get_full_config(); + $value = isset( $config[ $sub_config ] ) ? $config[ $sub_config ] : []; + $configs[ $sub_config ] = apply_filters( "mai_{$sub_config}_config", $value ); + + return $configs[ $sub_config ]; +} + +/** + * Returns the active child theme's full config. + * + * @access private + * + * @since 2.11.0 + * + * @return array + */ +function mai_get_full_config() { + static $config = null; + + if ( ! is_null( $config ) ) { + return $config; + } + + $config = require mai_get_dir() . 'config/_default.php'; + $theme = mai_get_active_theme(); + $theme = ( 'default' === $theme ) ? '_default' : $theme; + $path = mai_get_dir() . 'config/' . $theme . '.php'; + + if ( is_readable( $path ) ) { + $new = require $path; + $config = array_replace_recursive( $config, $new ); + if ( isset( $new['settings']['content-archives'] ) ) { + foreach ( $new['settings']['content-archives'] as $key => $settings ) { + if ( ! ( isset( $new['settings']['content-archives'][ $key ]['show'] ) && isset( $config['settings']['content-archives'][ $key ]['show'] ) ) ) { + continue; + } + $config['settings']['content-archives'][ $key ]['show'] = $new['settings']['content-archives'][ $key ]['show']; + } + } + if ( isset( $new['settings']['single-content'] ) ) { + foreach ( $new['settings']['single-content'] as $key => $settings ) { + if ( ! ( isset( $new['settings']['single-content'][ $key ]['show'] ) && isset( $config['settings']['single-content'][ $key ]['show'] ) ) ) { + continue; + } + $config['settings']['single-content'][ $key ]['show'] = $new['settings']['single-content'][ $key ]['show']; + } + } + } + + // Allow users to override from within actual child theme. + $child = get_stylesheet_directory() . '/config.php'; + + if ( is_readable( $child ) ) { + $new = require $child; + $config = array_replace_recursive( $config, $new ); + if ( isset( $new['settings']['content-archives'] ) ) { + foreach ( $new['settings']['content-archives'] as $key => $settings ) { + if ( ! ( isset( $new['settings']['content-archives'][ $key ]['show'] ) && isset( $config['settings']['content-archives'][ $key ]['show'] ) ) ) { + continue; + } + $config['settings']['content-archives'][ $key ]['show'] = $new['settings']['content-archives'][ $key ]['show']; + } + } + if ( isset( $new['settings']['single-content'] ) ) { + foreach ( $new['settings']['single-content'] as $key => $settings ) { + if ( ! ( isset( $new['settings']['single-content'][ $key ]['show'] ) && isset( $config['settings']['single-content'][ $key ]['show'] ) ) ) { + continue; + } + $config['settings']['single-content'][ $key ]['show'] = $new['settings']['single-content'][ $key ]['show']; + } + } + } + + $config = apply_filters( 'mai_config', $config ); + + return $config; +} + /** * Checks if Mai needs to load ACF Pro. * From 6d54094f9eafbc143e970d1423741838140fddcb Mon Sep 17 00:00:00 2001 From: JiveDig Date: Mon, 8 Jan 2024 15:49:21 -0500 Subject: [PATCH 2/2] Add requires headers --- mai-engine.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mai-engine.php b/mai-engine.php index 0af2ea289..f1f71f281 100644 --- a/mai-engine.php +++ b/mai-engine.php @@ -7,6 +7,8 @@ * GitHub Plugin URI: https://github.com/maithemewp/mai-engine/ * Description: The required plugin to power Mai child themes. * Version: 2.31.2 + * Requires at least: 6.4 + * Requires PHP: 7.4 * Author: BizBudding * Author URI: https://bizbudding.com/ * Text Domain: mai-engine