class-admin-notice-custom-css.php
1 week ago
class-admin-notices.php
1 week ago
class-admin.php
1 week ago
class-attachment-fields.php
1 week ago
class-columns.php
1 week ago
class-demo-content.php
1 week ago
class-extensions.php
1 week ago
class-gallery-attachment-modal.php
1 week ago
class-gallery-datasources.php
1 week ago
class-gallery-editor.php
1 week ago
class-gallery-metabox-fields.php
1 week ago
class-gallery-metabox-items.php
1 week ago
class-gallery-metabox-settings-helper.php
1 week ago
class-gallery-metabox-settings.php
1 week ago
class-gallery-metabox-template.php
1 week ago
class-gallery-metaboxes.php
1 week ago
class-menu.php
1 week ago
class-pro-promotion.php
1 week ago
class-settings.php
1 week ago
class-silent-installer-skin.php
1 week ago
class-trial-mode.php
1 week ago
demo-content-galleries.php
1 week ago
demo-content-images.php
1 week ago
index.php
1 week ago
pro-features.php
1 week ago
view-features.php
1 week ago
view-help-demos.php
1 week ago
view-help-getting-started.php
1 week ago
view-help-pro.php
1 week ago
view-help.php
1 week ago
view-system-info.php
1 week ago
class-extensions.php
216 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /* |
| 8 | * FooGallery Admin Extension class |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Admin_Extensions' ) ) { |
| 12 | |
| 13 | class FooGallery_Admin_Extensions { |
| 14 | |
| 15 | function __construct() { |
| 16 | add_action( 'init', array( $this, 'init' ) ); |
| 17 | add_action( 'deactivated_plugin', array( $this, 'handle_extensions_deactivation' ), 10, 2 ); |
| 18 | add_action( 'activated_plugin', array( $this, 'handle_extensions_activation' ), 10, 2 ); |
| 19 | } |
| 20 | |
| 21 | function init() { |
| 22 | add_action( 'admin_init', array( $this, 'handle_extension_action' ) ); |
| 23 | } |
| 24 | |
| 25 | function handle_extensions_deactivation( $plugin, $network_deactivating ) { |
| 26 | //make sure that if we are dealing with a FooGallery extension, that we deactivate it too |
| 27 | $api = new FooGallery_Extensions_API(); |
| 28 | $api->handle_wordpress_plugin_deactivation( $plugin ); |
| 29 | } |
| 30 | |
| 31 | function handle_extensions_activation( $plugin, $network_deactivating ) { |
| 32 | //make sure that if we are dealing with a FooGallery extension, that we deactivate it too |
| 33 | $api = new FooGallery_Extensions_API(); |
| 34 | $api->handle_wordpress_plugin_activation( $plugin ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Resolve and authorize an extension mutation for the current user. |
| 39 | * |
| 40 | * Menu visibility is intentionally separate from mutation authorization. |
| 41 | * Bundled feature toggles require the base FooGallery capability, while |
| 42 | * plugin-backed operations also follow WordPress plugin capabilities. |
| 43 | * |
| 44 | * @param string $action Extension action. |
| 45 | * @param string $extension_slug Registered extension slug. |
| 46 | * @param FooGallery_Extensions_API $api Optional API instance. |
| 47 | * @return array|false Authorized operation context, or false when denied. |
| 48 | */ |
| 49 | public static function authorize_extension_action( $action, $extension_slug, $api = null ) { |
| 50 | if ( ! current_user_can( 'manage_options' ) || ! in_array( $action, array( 'download', 'activate', 'deactivate' ), true ) ) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if ( null === $api ) { |
| 55 | $api = new FooGallery_Extensions_API(); |
| 56 | } |
| 57 | |
| 58 | $extension = $api->get_extension( $extension_slug ); |
| 59 | if ( ! $extension ) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | $source = foo_safe_get( $extension, 'source', false ); |
| 64 | if ( 'bundled' === $source ) { |
| 65 | if ( 'download' === $action ) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | return array( |
| 70 | 'extension' => $extension, |
| 71 | 'plugin_file' => false, |
| 72 | 'network_wide' => false, |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | if ( 'download' === $action ) { |
| 77 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | return array( |
| 82 | 'extension' => $extension, |
| 83 | 'plugin_file' => false, |
| 84 | 'network_wide' => false, |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | $plugin = $api->get_wordpress_plugin( $extension ); |
| 89 | if ( ! $plugin ) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | $plugin_file = $plugin['file']; |
| 94 | $network_wide = is_multisite() && is_network_admin(); |
| 95 | |
| 96 | if ( is_multisite() && ! $network_wide ) { |
| 97 | if ( ( 'activate' === $action && is_network_only_plugin( $plugin_file ) ) || is_plugin_active_for_network( $plugin_file ) ) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if ( ! current_user_can( $action . '_plugin', $plugin_file ) ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | return array( |
| 107 | 'extension' => $extension, |
| 108 | 'plugin_file' => $plugin_file, |
| 109 | 'network_wide' => $network_wide, |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Check whether the current user may run an extension mutation. |
| 115 | * |
| 116 | * @param string $action Extension action. |
| 117 | * @param string $extension_slug Registered extension slug. |
| 118 | * @param FooGallery_Extensions_API $api Optional API instance. |
| 119 | * @return bool Whether the action is authorized. |
| 120 | */ |
| 121 | public static function can_manage_extension_action( $action, $extension_slug, $api = null ) { |
| 122 | return false !== self::authorize_extension_action( $action, $extension_slug, $api ); |
| 123 | } |
| 124 | |
| 125 | function handle_extension_action() { |
| 126 | |
| 127 | $action = sanitize_key( safe_get_from_request( 'action' ) ); |
| 128 | $extension_slug = sanitize_key( safe_get_from_request( 'extension' ) ); |
| 129 | $has_error = safe_get_from_request( 'has_error' ); |
| 130 | $api = null; |
| 131 | $authorization = false; |
| 132 | |
| 133 | if ( !empty( $extension_slug ) || $has_error ) { |
| 134 | if ( !check_admin_referer( 'foogallery_extension_action' ) ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | $api = new FooGallery_Extensions_API(); |
| 139 | if ( $has_error && ! empty( $action ) ) { |
| 140 | $authorization = false; |
| 141 | } else { |
| 142 | $authorization_action = $has_error ? 'deactivate' : $action; |
| 143 | $authorization = self::authorize_extension_action( $authorization_action, $extension_slug, $api ); |
| 144 | } |
| 145 | |
| 146 | if ( false === $authorization ) { |
| 147 | wp_die( |
| 148 | esc_html__( 'You do not have permission to manage FooGallery features.', 'foogallery' ), |
| 149 | '', |
| 150 | array( 'response' => 403 ) |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if ( ( 'download' === $action || 'activate' === $action || 'deactivate' === $action ) && $extension_slug ) { |
| 156 | $fatal_error_redirect = remove_query_arg( 'action' ); |
| 157 | wp_redirect( add_query_arg( 'has_error', 'yes', $fatal_error_redirect ) ); // we'll override this later if the plugin can be included without fatal error |
| 158 | ob_start(); |
| 159 | |
| 160 | switch ( $action ) { |
| 161 | case 'download': |
| 162 | $result = $api->download( $extension_slug ); |
| 163 | break; |
| 164 | case 'activate': |
| 165 | $result = $api->activate( $extension_slug, true, $authorization['network_wide'] ); |
| 166 | break; |
| 167 | case 'deactivate': |
| 168 | $result = $api->deactivate( $extension_slug, true, false, $authorization['network_wide'] ); |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | //if we get here then no fatal error - cool! |
| 173 | if ( ob_get_length() > 0 ) { |
| 174 | ob_end_clean(); |
| 175 | } |
| 176 | |
| 177 | //store the result in a short-lived transient |
| 178 | if ( isset($result) ) { |
| 179 | set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 ); |
| 180 | } |
| 181 | |
| 182 | //first, remove unwanted query args |
| 183 | $redirect_url = remove_query_arg( array( 'extension', 'action' ) ); |
| 184 | //then add a query arg for our message |
| 185 | $redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url ); |
| 186 | //finally, allow extensions to override their own redirect |
| 187 | $redirect_url = apply_filters( 'foogallery_extensions_redirect_url-' . $extension_slug, $redirect_url, $action ); |
| 188 | |
| 189 | //redirect to this page, so the plugin can be properly activated/deactivated etc |
| 190 | if ( $redirect_url ) { |
| 191 | wp_redirect( $redirect_url ); |
| 192 | die(); |
| 193 | } |
| 194 | } else if ( $has_error ) { |
| 195 | $api->deactivate( $extension_slug, true, false, $authorization['network_wide'] ); |
| 196 | |
| 197 | $result = array( |
| 198 | 'message' => __( 'The extension could not be activated due to an error!', 'foogallery' ), |
| 199 | 'type' => 'error', |
| 200 | ); |
| 201 | |
| 202 | set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 ); |
| 203 | |
| 204 | $api->add_to_error_extensions( $extension_slug, __( 'Activation Error!', 'foogallery' ) ); |
| 205 | |
| 206 | //first, remove unwanted query args |
| 207 | $redirect_url = remove_query_arg( array( 'extension', 'action', 'has_error' ) ); |
| 208 | //then add a query arg for our message |
| 209 | $redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url ); |
| 210 | |
| 211 | wp_redirect( $redirect_url ); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 |