class-extension.php
1 week ago
class-extensions-api.php
1 week ago
class-extensions-loader.php
1 week ago
class-extensions-loader.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'FooGallery_Extensions_Loader' ) ) { |
| 8 | class FooGallery_Extensions_Loader { |
| 9 | |
| 10 | function __construct() { |
| 11 | add_action( 'plugins_loaded', array( $this, 'load_active_extensions' ) ); |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Load all FooGallery extensions that have been activated. |
| 16 | * For each extension, create an instance of the extension class and add it to a global extensions array |
| 17 | */ |
| 18 | function load_active_extensions() { |
| 19 | $action = foo_safe_get( $_POST, 'action'); |
| 20 | if ( 'deactivate' === $action ) { return; } |
| 21 | |
| 22 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 23 | require_once(ABSPATH . 'wp-admin/includes/screen.php'); |
| 24 | } |
| 25 | |
| 26 | $api = new FooGallery_Extensions_API(); |
| 27 | $active_extensions = $api->get_loadable_extensions(); |
| 28 | foreach ( $active_extensions as $slug => $class ) { |
| 29 | try { |
| 30 | $this->load_extension( $slug, $class ); |
| 31 | } |
| 32 | catch (Exception $e) { |
| 33 | $error = $e; |
| 34 | $something = $error; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | $this->load_requested_album_extension( $api ); |
| 39 | |
| 40 | //What if no extensions were loaded? |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Load albums when the current admin request is already for an album. |
| 45 | * |
| 46 | * This keeps existing album edit/save requests from reaching WordPress core |
| 47 | * with an unregistered post type when extension state options drift. |
| 48 | * |
| 49 | * @param FooGallery_Extensions_API $api The extensions API. |
| 50 | */ |
| 51 | private function load_requested_album_extension( $api ) { |
| 52 | if ( ! is_admin() || ! $this->is_album_admin_request() ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $extension = $api->get_extension( 'albums' ); |
| 57 | $class = foo_safe_get( $extension, 'class' ); |
| 58 | |
| 59 | if ( ! empty( $class ) ) { |
| 60 | $this->load_extension( 'albums', $class ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Determine whether the current admin request is for a FooGallery album. |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | private function is_album_admin_request() { |
| 70 | $post_type = ''; |
| 71 | if ( isset( $_REQUEST['post_type'] ) && ! is_array( $_REQUEST['post_type'] ) ) { |
| 72 | $post_type = sanitize_key( wp_unslash( $_REQUEST['post_type'] ) ); |
| 73 | } |
| 74 | |
| 75 | if ( FOOGALLERY_CPT_ALBUM === $post_type ) { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | $post_id = 0; |
| 80 | if ( isset( $_REQUEST['post'] ) && ! is_array( $_REQUEST['post'] ) ) { |
| 81 | $post_id = absint( wp_unslash( $_REQUEST['post'] ) ); |
| 82 | } elseif ( isset( $_REQUEST['post_ID'] ) && ! is_array( $_REQUEST['post_ID'] ) ) { |
| 83 | $post_id = absint( wp_unslash( $_REQUEST['post_ID'] ) ); |
| 84 | } |
| 85 | |
| 86 | return $post_id > 0 && FOOGALLERY_CPT_ALBUM === get_post_type( $post_id ); |
| 87 | } |
| 88 | |
| 89 | function load_extension( $slug, $class ) { |
| 90 | global $foogallery_extensions; |
| 91 | global $foogallery_currently_loading; |
| 92 | if ( is_null( $foogallery_extensions ) ) { |
| 93 | $foogallery_extensions = array(); |
| 94 | } |
| 95 | if ( class_exists( $class ) && !array_key_exists( $slug, $foogallery_extensions ) ) { |
| 96 | $foogallery_currently_loading = $slug; |
| 97 | $instance = new $class(); |
| 98 | $foogallery_extensions[ $slug ] = $instance; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function handle_load_exceptions( $errno, $errstr, $errfile, $errline ) { |
| 103 | global $foogallery_currently_loading; |
| 104 | $api = new FooGallery_Extensions_API(); |
| 105 | $api->deactivate( $foogallery_currently_loading, false, true ); |
| 106 | |
| 107 | //don't execute PHP internal error handler |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 |