as used on dashboard. */ public function get_imgopt_status_notice() { if ( $this->imgopt_active() && apply_filters( 'autoptimize_filter_imgopt_status_shortpixel', true ) ) { $_imgopt_notice = ''; $_stat = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_provider_stat', '' ); $_site_host = AUTOPTIMIZE_SITE_DOMAIN; $_imgopt_upsell = 'https://misc.optimizingmatters.com/partners/?from=aofree&partner=shortpixelupsell'; $_imgopt_assoc = 'https://shortpixel.helpscoutdocs.com/article/94-how-to-associate-a-domain-to-my-account'; $_imgopt_unreach = 'https://shortpixel.helpscoutdocs.com/article/148-why-are-my-images-redirected-from-cdn-shortpixel-ai'; if ( is_array( $_stat ) ) { if ( 1 == $_stat['Status'] ) { // translators: "add more credits" will appear in a "a href". $_imgopt_notice = sprintf( esc_html__( 'Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website %4$sor consider using %3$sAutoptimize Pro%2$s which comes with (nearly) unlimited image optimization%5$s but also automated critical CSS and extra booster options.', 'autoptimize' ), '', '', '', '', '' ); } elseif ( -1 == $_stat['Status'] || -2 == $_stat['Status'] ) { // translators: "add more credits" will appear in a "a href". $_imgopt_notice = sprintf( esc_html__( 'Your ShortPixel image optimization and CDN quota has been exhausted, %1$sadd more credits%2$s to continue to quickly deliver optimized images on your website %4$sor consider using %3$sAutoptimize Pro%2$s which comes with (nearly) unlimited image optimization%5$s but also automated critical CSS and extra booster options.', 'autoptimize' ), '', '', '', '', '' ); // translators: "associate your domain" will appear in a "a href". $_imgopt_notice = $_imgopt_notice . ' ' . sprintf( esc_html__( 'If you have enough CDN quota remaining, then you may need to %1$sassociate your domain%2$s to your Shortpixel account.', 'autoptimize' ), '', '' ); } elseif ( -3 == $_stat['Status'] ) { // translators: "check the documentation here" will appear in a "a href". $_imgopt_notice = sprintf( esc_html__( 'It seems ShortPixel image optimization is not able to fetch images from your site, %1$scheck the documentation here%2$s for more information', 'autoptimize' ), '', '' ); } else { $_imgopt_upsell = 'https://misc.optimizingmatters.com/partners/?from=aofree&partner=shortpixelupsell'; // translators: "log in to check your account" will appear in a "a href". $_imgopt_notice = sprintf( esc_html__( 'Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize' ), '', '' ); } // add info on freshness + refresh link if status is not 2 (good shape). if ( 2 != $_stat['Status'] ) { $_imgopt_stats_refresh_url = add_query_arg( array( 'page' => 'autoptimize_imgopt', 'refreshImgProvStats' => '1', ), admin_url( 'options-general.php' ) ); if ( $_stat && array_key_exists( 'timestamp', $_stat ) && ! empty( $_stat['timestamp'] ) ) { $_imgopt_stats_last_run = esc_html__( 'based on status at ', 'autoptimize' ) . date_i18n( autoptimizeOptionWrapper::get_option( 'time_format' ), $_stat['timestamp'] ); } else { $_imgopt_stats_last_run = esc_html__( 'based on previously fetched data', 'autoptimize' ); } $_imgopt_notice .= ' (' . $_imgopt_stats_last_run . ', '; // translators: "here to refresh" links to the Autoptimize Extra page and forces a refresh of the img opt stats. $_imgopt_notice .= sprintf( esc_html__( 'you can click %1$shere to refresh your quota status%2$s', 'autoptimize' ), '', ').' ); } // and make the full notice filterable. $_imgopt_notice = apply_filters( 'autoptimize_filter_imgopt_notice', $_imgopt_notice ); return array( 'status' => $_stat['Status'], 'notice' => $_imgopt_notice, ); } } return false; } public static function get_imgopt_status_notice_wrapper() { // needed for notice being shown in autoptimizeCacheChecker.php. $self = new self(); return $self->get_imgopt_status_notice(); } /** * Get img provider stats (used to display notice). * * @param bool $_refresh Should the stats be forcefully refreshed or not. */ public function query_img_provider_stats( $_refresh = false ) { if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_1'] ) && apply_filters( 'autoptimize_filter_imgopt_status_shortpixel', true ) ) { $url = ''; $stat_dom = 'https://no-cdn.shortpixel.ai/'; $endpoint = $stat_dom . 'read-domain/'; $domain = AUTOPTIMIZE_SITE_DOMAIN; // make sure parse_url result makes sense, keeping $url empty if not. if ( $domain && ! empty( $domain ) ) { $url = $endpoint . $domain; if ( true === $_refresh ) { $url = $url . '/refresh'; } } $url = apply_filters( 'autoptimize_filter_imgopt_stat_url', $url ); // only do the remote call if $url is not empty to make sure no parse_url // weirdness results in useless calls. if ( ! empty( $url ) ) { $response = wp_remote_get( $url ); if ( ! is_wp_error( $response ) ) { if ( '200' == wp_remote_retrieve_response_code( $response ) ) { $stats = json_decode( wp_remote_retrieve_body( $response ), true ); autoptimizeOptionWrapper::update_option( 'autoptimize_imgopt_provider_stat', $stats ); } } } } } public static function get_img_provider_stats() { // wrapper around query_img_provider_stats() so we can get to $this->options from cronjob() in autoptimizeCacheChecker. $self = new self(); return $self->query_img_provider_stats(); } /** * Determines and returns the service launch status. * * @return bool */ public function launch_ok() { static $launch_status = null; if ( null === $launch_status ) { $avail_imgopt = ''; if ( is_array( $this->options ) && array_key_exists( 'availabilities', $this->options ) && is_array( $this->options['availabilities'] ) && array_key_exists( 'extra_imgopt', $this->options['availabilities'] ) ) { $avail_imgopt = $this->options['availabilities']['extra_imgopt']; } $magic_number = intval( substr( md5( parse_url( AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST ) ), 0, 3 ), 16 ); $has_launched = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_launched', '' ); $launch_status = false; if ( $has_launched || ( is_array( $avail_imgopt ) && array_key_exists( 'launch-threshold', $avail_imgopt ) && $magic_number < $avail_imgopt['launch-threshold'] ) ) { $launch_status = true; if ( ! $has_launched ) { autoptimizeOptionWrapper::update_option( 'autoptimize_imgopt_launched', 'on' ); } } } return $launch_status; } public static function launch_ok_wrapper() { // needed for "plug" notice in autoptimizeMain.php. $self = new self(); return $self->launch_ok(); } public function get_imgopt_provider_userstatus() { static $_provider_userstatus = null; if ( is_null( $_provider_userstatus ) ) { $_stat = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_provider_stat', '' ); if ( is_array( $_stat ) ) { if ( array_key_exists( 'Status', $_stat ) ) { $_provider_userstatus['Status'] = $_stat['Status']; } else { // if no stats then we assume all is well. $_provider_userstatus['Status'] = 2; } if ( array_key_exists( 'timestamp', $_stat ) ) { $_provider_userstatus['timestamp'] = $_stat['timestamp']; } else { // if no timestamp then we return "". $_provider_userstatus['timestamp'] = ''; } } else { // no provider_stat yet, assume/ return all OK. $_provider_userstatus['Status'] = 2; $_provider_userstatus['timestamp'] = ''; } } return $_provider_userstatus; } }