| @@ -1,78 +1,99 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); | |
| 2 | +defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | - * A wrapper to easily get imagify option | |
| 5 | + * A wrapper to easily get imagify option. | |
| 6 | 6 | * |
| 7 | 7 | * @since 1.0 |
| 8 | + * @since 1.7 The $default parameter was removed. | |
| 8 | 9 | * |
| 9 | - * @param string $option The option name | |
| 10 | - * @param bool $default The default value of option | |
| 11 | - * @return mixed The option value | |
| 10 | + * @param string $key The option name. | |
| 11 | + * @return mixed The option value. | |
| 12 | 12 | */ |
| 13 | -function get_imagify_option( $option, $default = false ) { | |
| 14 | - /** | |
| 15 | - * Pre-filter any Imagify option before read | |
| 16 | - * | |
| 17 | - * @since 1.0 | |
| 18 | - * | |
| 19 | - * @param variant $default The default value | |
| 20 | - */ | |
| 21 | - $value = apply_filters( 'pre_get_imagify_option_' . $option, NULL, $default ); | |
| 13 | +function get_imagify_option( $key ) { | |
| 14 | + return Imagify_Options::get_instance()->get( $key ); | |
| 15 | +} | |
| 22 | 16 | |
| 23 | - if ( NULL !== $value ) { | |
| 24 | - return $value; | |
| 25 | - } | |
| 26 | - | |
| 27 | - $plugins = get_site_option( 'active_sitewide_plugins'); | |
| 28 | - $options = isset( $plugins[ IMAGIFY_SLUG . '/imagify.php' ] ) ? get_site_option( IMAGIFY_SETTINGS_SLUG ) : get_option( IMAGIFY_SETTINGS_SLUG ); | |
| 29 | - $value = isset( $options[ $option ] ) && $options[ $option ] !== '' ? $options[ $option ] : $default; | |
| 30 | - | |
| 31 | - if ( 'api_key' === $option && ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) ) { | |
| 32 | - $value = IMAGIFY_API_KEY; | |
| 33 | - } | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * Filter any Imagify option after read | |
| 37 | - * | |
| 38 | - * @since 1.0 | |
| 39 | - * | |
| 40 | - * @param variant $default The default value | |
| 41 | - */ | |
| 42 | - return apply_filters( 'get_imagify_option_' . $option, $value, $default ); | |
| 17 | +/** | |
| 18 | + * Update an Imagify option. | |
| 19 | + * | |
| 20 | + * @since 1.6 | |
| 21 | + * @author Remy Perona | |
| 22 | + * | |
| 23 | + * @param string $key The option name. | |
| 24 | + * @param mixed $value The value of the option. | |
| 25 | + */ | |
| 26 | +function update_imagify_option( $key, $value ) { | |
| 27 | + Imagify_Options::get_instance()->set( $key, $value ); | |
| 43 | 28 | } |
| 44 | 29 | |
| 45 | 30 | /** |
| 46 | - * Determine if the Imagify API key is valid | |
| 31 | + * Autoload network options and put them in cache. | |
| 47 | 32 | * |
| 48 | - * @since 1.0 | |
| 33 | + * @since 1.7 | |
| 34 | + * @author Grégory Viguier | |
| 35 | + * @source SecuPress | |
| 49 | 36 | * |
| 50 | - * @return bool True if the API key is valid | |
| 37 | + * @param array $option_names A list of option names to cache. | |
| 38 | + * @param string|array $prefixes A prefix for the option names. Handy for transients for example (`_site_transient_` and `_site_transient_timeout_`). | |
| 51 | 39 | */ |
| 52 | -function imagify_valid_key() { | |
| 53 | - static $imagify_valid_key = null; | |
| 54 | - | |
| 55 | - if ( null !== $imagify_valid_key ) { | |
| 56 | - return $imagify_valid_key; | |
| 40 | +function imagify_load_network_options( $option_names, $prefixes = '' ) { | |
| 41 | + global $wpdb; | |
| 42 | + | |
| 43 | + $prefixes = (array) $prefixes; | |
| 44 | + | |
| 45 | + if ( ! $option_names || count( $option_names ) * count( $prefixes ) === 1 ) { | |
| 46 | + return; | |
| 57 | 47 | } |
| 58 | - | |
| 59 | - if ( ! get_imagify_option( 'api_key', false ) ) { | |
| 60 | - $imagify_valid_key = false; | |
| 61 | - return false; | |
| 48 | + | |
| 49 | + // Get values. | |
| 50 | + $not_exist = []; | |
| 51 | + $option_names = array_flip( array_flip( $option_names ) ); | |
| 52 | + $options = ''; | |
| 53 | + | |
| 54 | + foreach ( $prefixes as $prefix ) { | |
| 55 | + $options .= "'$prefix" . implode( "','$prefix", esc_sql( $option_names ) ) . "',"; | |
| 62 | 56 | } |
| 63 | - | |
| 64 | - if ( get_site_transient( 'imagify_check_licence_1' ) ) { | |
| 65 | - $imagify_valid_key = true; | |
| 66 | - return true; | |
| 57 | + | |
| 58 | + $options = rtrim( $options, ',' ); | |
| 59 | + | |
| 60 | + if ( is_multisite() ) { | |
| 61 | + $network_id = function_exists( 'get_current_network_id' ) ? get_current_network_id() : (int) $wpdb->siteid; | |
| 62 | + $cache_prefix = "$network_id:"; | |
| 63 | + $notoptions_key = "$network_id:notoptions"; | |
| 64 | + $cache_group = 'site-options'; | |
| 65 | + $results = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key as name, meta_value as value FROM $wpdb->sitemeta WHERE meta_key IN ( $options ) AND site_id = %d", $network_id ), OBJECT_K ); // WPCS: unprepared SQL ok. | |
| 66 | + } else { | |
| 67 | + $cache_prefix = ''; | |
| 68 | + $notoptions_key = 'notoptions'; | |
| 69 | + $cache_group = 'options'; | |
| 70 | + $results = $wpdb->get_results( "SELECT option_name as name, option_value as value FROM $wpdb->options WHERE option_name IN ( $options )", OBJECT_K ); // WPCS: unprepared SQL ok. | |
| 67 | 71 | } |
| 68 | - | |
| 69 | - if ( is_wp_error( get_imagify_user() ) ) { | |
| 70 | - $imagify_valid_key = false; | |
| 71 | - return false; | |
| 72 | + | |
| 73 | + foreach ( $prefixes as $prefix ) { | |
| 74 | + foreach ( $option_names as $option_name ) { | |
| 75 | + $option_name = $prefix . $option_name; | |
| 76 | + | |
| 77 | + if ( isset( $results[ $option_name ] ) ) { | |
| 78 | + // Cache the value. | |
| 79 | + $value = $results[ $option_name ]->value; | |
| 80 | + $value = maybe_unserialize( $value ); | |
| 81 | + wp_cache_set( "$cache_prefix$option_name", $value, $cache_group ); | |
| 82 | + } else { | |
| 83 | + // No value. | |
| 84 | + $not_exist[ $option_name ] = true; | |
| 85 | + } | |
| 86 | + } | |
| 72 | 87 | } |
| 73 | - | |
| 74 | - $imagify_valid_key = true; | |
| 75 | - set_site_transient( 'imagify_check_licence_1', true, YEAR_IN_SECONDS ); | |
| 76 | - | |
| 77 | - return true; | |
| 78 | -} | |
| 88 | + | |
| 89 | + if ( ! $not_exist ) { | |
| 90 | + return; | |
| 91 | + } | |
| 92 | + | |
| 93 | + // Cache the options that don't exist in the DB. | |
| 94 | + $notoptions = wp_cache_get( $notoptions_key, $cache_group ); | |
| 95 | + $notoptions = is_array( $notoptions ) ? $notoptions : []; | |
| 96 | + $notoptions = array_merge( $notoptions, $not_exist ); | |
| 97 | + | |
| 98 | + wp_cache_set( $notoptions_key, $notoptions, $cache_group ); | |
| 99 | +} | |