public/blog/wp-content/plugins/types/application/controllers/main.php line 171

  1. <?php
  2. use OTGS\Toolset\Types\TypeRegistration\Controller;
  3. /** @noinspection PhpFullyQualifiedNameUsageInspection */
  4. use OTGS\Toolset\Types\Controller\Compatibility\Gutenberg;
  5. /**
  6.  * Main Types controller.
  7.  *
  8.  * Determines if we're in admin or front-end mode or if an AJAX call is being performed. Handles tasks that are common
  9.  * to all three modes, if there are any.
  10.  *
  11.  * @since 2.0
  12.  */
  13. final class Types_Main {
  14.     private static $instance;
  15.     public static function get_instance() {
  16.         if( null == self::$instance ) {
  17.             self::$instance = new self();
  18.         }
  19.         return self::$instance;
  20.     }
  21.     public static function initialize() {
  22.         self::get_instance();
  23.     }
  24.     private function __construct() {
  25.         // Indicate that m2m can be activated with this plugin. This needs to happen very early, even before Toolset
  26.         // Common bootstrap is executed.
  27.         add_filter'toolset_is_m2m_ready''__return_true' );
  28.         add_action'after_setup_theme', array( $this'after_setup_theme' ), 10 );
  29.         // important to be 11, because we register our post types on 10 (we should consider loading them earlier)
  30.         add_action'init', array( $this'on_init' ), 11 );
  31.         // attachment deletion
  32.         add_action'delete_attachment', static function( $attachment_post_id ) {
  33.             if( class_exists'Types_Media_Service' ) ) {
  34.                 $media_service = new Types_Media_Service();
  35.                 $media_service->delete_resized_images_by_attachment_id$attachment_post_id );
  36.             }
  37.         } );
  38.     }
  39.     /**
  40.      * Determine in which mode we are and initialize the right dedicated controller.
  41.      *
  42.      * @since 2.0
  43.      */
  44.     public function on_init() {
  45.         if( is_admin() ) {
  46.             if( defined'DOING_AJAX' ) ) {
  47.                 $this->mode self::MODE_AJAX;
  48.                 Types_Ajax::initialize();
  49.             } else {
  50.                 $this->mode self::MODE_ADMIN;
  51.                 $admin_controller = new Types_Admin();
  52.                 $admin_controller->initialize();
  53.             }
  54.         } else {
  55.             $this->mode self::MODE_FRONTEND;
  56.             Types_Frontend::initialize();
  57.         }
  58.         $dic toolset_dic();
  59.         $m2m $dic->makeTypes_M2M::class );
  60.         $m2m->initialize();
  61.         // Initialize the "types" shortcode in all contexts.
  62.         $factory = new Types_Shortcode_Factory();
  63.         $types_shortcode $factory->get_shortcode'types' );
  64.         if ( $types_shortcode ) {
  65.             add_shortcode'types', array( $types_shortcode'render' ) );
  66.         };
  67.         if ( in_array$this->mode, [ self::MODE_ADMINself::MODE_AJAX ], true ) ) {
  68.             /** @var \OTGS\Toolset\Types\TypeRegistration\Controller $type_registration_controller */
  69.             /** @noinspection PhpUnhandledExceptionInspection */
  70.             $type_registration_controller $dic->makeController::class );
  71.             $type_registration_controller->initialize();
  72.             /** @var Gutenberg $gutenberg */
  73.             /** @noinspection PhpUnhandledExceptionInspection */
  74.             $gutenberg $dic->makeGutenberg::class );
  75.             $gutenberg->initialize();
  76.         }
  77.     }
  78.     /**
  79.      * @var string One of the MODE_* constants.
  80.      */
  81.     private $mode self::MODE_UNDEFINED;
  82.     const MODE_UNDEFINED '';
  83.     const MODE_AJAX 'ajax';
  84.     const MODE_ADMIN 'admin';
  85.     const MODE_FRONTEND 'frontend';
  86.     /**
  87.      * Get current plugin mode.
  88.      *
  89.      * Possible values are:
  90.      * - MODE_UNDEFINED before the main controller initialization is completed
  91.      * - MODE_AJAX when doing an AJAX request
  92.      * - MODE_ADMIN when showing a WP admin page
  93.      * - MODE_FRONTEND when rendering a frontend page
  94.      *
  95.      * @return string
  96.      * @since 2.1
  97.      */
  98.     public function get_plugin_mode() {
  99.         return $this->mode;
  100.     }
  101.     /**
  102.      * Set current plugin mode.
  103.      *
  104.      * @param string $new_mode the new plugin mode
  105.      * @return bool TRUE if set is succesfully done, FALSE otherwise
  106.      * @since 2.2
  107.      */
  108.     public function set_plugin_mode$new_mode self::MODE_UNDEFINED ) {
  109.         if ( !in_array$new_mode, array( self::MODE_UNDEFINEDself::MODE_AJAXself::MODE_ADMINself::MODE_FRONTEND ) ) ){
  110.             return false;
  111.         }
  112.         $this->mode $new_mode;
  113.         return true;
  114.     }
  115.     /**
  116.      * Determine whether a WP admin page is being loaded.
  117.      *
  118.      * Note that the behaviour differs from the native is_admin() which will return true also for AJAX requests.
  119.      *
  120.      * @return bool
  121.      * @since 2.1
  122.      */
  123.     public function is_admin() {
  124.         return ( $this->get_plugin_mode() === self::MODE_ADMIN );
  125.     }
  126.     /**
  127.      * Early loading actions.
  128.      *
  129.      * @since 2.0
  130.      * @since m2m Load the shortcodes generator
  131.      */
  132.     public function after_setup_theme() {
  133.         
  134.         // Indicate that m2m can be activated with this plugin.
  135.         add_filter'toolset_is_m2m_ready''__return_true' );
  136.         // Initialize the Toolset Common library
  137.         $toolset_common_bootstrap Toolset_Common_Bootstrap::get_instance();
  138.         $this->setup_autoloader();
  139.         $this->setup_auryn();
  140.         // If an AJAX callback handler needs other assets, it should initialize the asset manager by itself.
  141.         if( $this->get_plugin_mode() !== self::MODE_AJAX ) {
  142.             Types_Assets::get_instance()->initialize_scripts_and_styles();
  143.         }
  144.         // Handle embedded plugin mode
  145.         Types_Embedded::initialize();
  146.         Types_Api::initialize();
  147.         $dic types_dic();
  148.         $interop_mediator $dic->make\OTGS\Toolset\Types\Controller\Interop\InteropMediator::class );
  149.         $interop_mediator->initialize();
  150.         $plugin_cache $dic->make'\OTGS\Toolset\Types\Controller\Cache' );
  151.         $plugin_cache->initialize();
  152.         // Load the shortcodes generator and the blocks section.
  153.         $toolset_common_sections = array( 'toolset_shortcode_generator'Toolset_Common_Bootstrap::TOOLSET_BLOCKS );
  154.         $toolset_common_bootstrap->load_sections$toolset_common_sections );
  155.         $types_shortcode_generator = new Types_Shortcode_Generator();
  156.         $types_shortcode_generator->initialize();
  157.     }
  158.     private function setup_autoloader() {
  159.         // It is possible to regenerate the classmap with Zend framework.
  160.         //
  161.         // See the "recreate_classmap.sh" script in the plugin root directory.
  162.         $classmap = include( TYPES_ABSPATH '/application/autoload_classmap.php' );
  163.         // Use Toolset_Common_Autoloader
  164.         do_action'toolset_register_classmap'$classmap );
  165.     }
  166.     /**
  167.      * In some cases, it may not be clear what legacy files are includes and what aren't.
  168.      *
  169.      * This method should make sure all is covered (add files when needed). Use only when necessary.
  170.      *
  171.      * @since 2.0
  172.      */
  173.     public function require_legacy_functions() {
  174.         require_once WPCF_INC_ABSPATH '/fields.php';
  175.     }
  176.     /**
  177.      * Configure Auryn so that it is able to inject our singleton instances properly.
  178.      *
  179.      * @since 3.2.2
  180.      */
  181.     private function setup_auryn() {
  182.         // This will initialize the DIC if it hasn't been done yet
  183.         $dic types_dic();
  184.         // See utility/dic.php in Toolset Common for details. We're doing the same for Types here.
  185.         $singleton_delegates = array(
  186.             '\Types_Asset_Manager' => function() {
  187.                 return Types_Asset_Manager::get_instance();
  188.             }
  189.         );
  190.         foreach( $singleton_delegates as $class_name => $callback ) {
  191.             /** @noinspection PhpUnhandledExceptionInspection */
  192.             $dic->delegate$class_name, function() use( $callback$dic ) {
  193.                 $instance $callback();
  194.                 $dic->share$instance );
  195.                 return $instance;
  196.             });
  197.         }
  198.     }
  199. }