notices = new Tiny_Notices( $this ); new Tiny_Diagnostics( $this ); } private function init_compressor() { $this->compressor = Tiny_Compress::create( $this->get_api_key(), $this->get_method( 'after_compress_callback' ) ); } public function get_absolute_url() { return get_admin_url( null, 'options-general.php?page=tinify' ); } public function xmlrpc_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { Tiny_Logger::error( $e->getMessage() ); } } public function cli_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { Tiny_Logger::error( $e->getMessage() ); } } public function ajax_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { Tiny_Logger::error( $e->getMessage() ); } add_action( 'wp_ajax_tiny_image_sizes_notice', $this->get_method( 'image_sizes_notice' ) ); add_action( 'wp_ajax_tiny_account_status', $this->get_method( 'account_status' ) ); add_action( 'wp_ajax_tiny_settings_create_api_key', $this->get_method( 'create_api_key' ) ); add_action( 'wp_ajax_tiny_settings_update_api_key', $this->get_method( 'update_api_key' ) ); } public function rest_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { Tiny_Logger::error( $e->getMessage() ); } } public function admin_init() { try { $this->init_compressor(); } catch ( Tiny_Exception $e ) { $this->notices->show( 'compressor_exception', esc_html( $e->getMessage() ), 'error', false ); } if ( current_user_can( 'manage_options' ) && ! Tiny_Onboarding::is_onboarding_page() ) { $this->setup_incomplete_checks(); } $field = self::get_prefixed_name( 'compression_timing' ); register_setting( 'tinify', $field ); $field = self::get_prefixed_name( 'sizes' ); register_setting( 'tinify', $field ); $field = self::get_prefixed_name( 'resize_original' ); register_setting( 'tinify', $field ); $field = self::get_prefixed_name( 'backup' ); register_setting( 'tinify', $field ); $field = self::get_prefixed_name( 'preserve_data' ); register_setting( 'tinify', $field ); $field = self::get_prefixed_name( 'convert_format' ); register_setting( 'tinify', $field ); $field = self::get_prefixed_name( 'logging_enabled' ); register_setting( 'tinify', $field ); } public function admin_menu() { /* Create link to new settings page from media settings page. */ add_settings_section( 'section_end', '', $this->get_method( 'render_settings_moved' ), 'media' ); add_options_page( __( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' ), esc_html__( 'TinyPNG', 'tiny-compress-images' ), 'manage_options', 'tinify', array( $this, 'add_options_to_page' ) ); } public function add_options_to_page() { include __DIR__ . '/views/settings.php'; } public function image_sizes_notice() { check_ajax_referer( 'tiny-compress' ); if ( current_user_can( 'manage_options' ) ) { $selected_sizes = isset( $_GET['image_sizes_selected'] ) ? intval( $_GET['image_sizes_selected'] ) : 0; $resize_original = isset( $_GET['resize_original'] ); $compress_wr2x = isset( $_GET['compress_wr2x'] ); $convert = isset( $_GET['convert'] ); echo '
'; if ( $active_sizes_count < 1 ) { esc_html_e( 'With these settings no images will be compressed.', 'tiny-compress-images' ); } else { $credits = self::get_credits_per_image( $active_sizes_count, $resize_original_enabled, $compress_wr2x, $conversion_enabled ); printf( esc_html( /* translators: %s: number of image sizes */ _n( '%s size selected.', '%s sizes selected.', $active_sizes_count, 'tiny-compress-images' ) ), esc_html( number_format_i18n( $active_sizes_count ) ) ); echo ' '; printf( esc_html( /* translators: %s: number of credits */ _n( 'Each new image uses %s credit.', 'Each new image uses %s credits.', $credits, 'tiny-compress-images' ) ), esc_html( number_format_i18n( $credits ) ) ); } echo '
'; if ( self::wr2x_active() ) { echo '';
esc_html_e(
'If selected, retina sizes will be compressed when generated by WP Retina 2x',
'tiny-compress-images'
);
echo '
';
esc_html_e(
'Each retina size will count as an additional compression.',
'tiny-compress-images'
);
echo '
'; printf( esc_html( /* translators: %s: number of credits */ _n( 'With your settings, each new image uses %s credit.', 'With your settings, each new image uses %s credits.', $credits, 'tiny-compress-images' ) ), esc_html( number_format_i18n( $credits ) ) ); echo '
'; } public function render_preserve_input( $name, $description ) { $data = array( 'id' => sprintf( self::get_prefixed_name( 'preserve_data_%s' ), $name ), 'field' => sprintf( self::get_prefixed_name( 'preserve_data[%s]' ), $name ), 'checked' => $this->get_preserve_enabled( $name ), 'label' => $description, ); include plugin_dir_path( __FILE__ ) . 'views/settings-original-image-preserve.php'; } public function render_resize_input( $name ) { $settings = get_option( self::get_prefixed_name( 'resize_original' ) ); $data = array( 'id' => sprintf( self::get_prefixed_name( 'resize_original_%s' ), $name ), 'field' => sprintf( self::get_prefixed_name( 'resize_original[%s]' ), $name ), 'value' => isset( $settings[ $name ] ) ? $settings[ $name ] : '2048', ); include plugin_dir_path( __FILE__ ) . 'views/settings-original-image-original.php'; } public function get_compression_count() { $field = self::get_prefixed_name( 'status' ); return get_option( $field ); } public function limit_reached() { $this->compressor->get_compression_count(); return $this->compressor->limit_reached(); } public function get_remaining_credits() { $field = self::get_prefixed_name( 'remaining_credits' ); return get_option( $field ); } public function get_paying_state() { $field = self::get_prefixed_name( 'paying_state' ); return get_option( $field ); } public function is_on_free_plan() { return self::get_paying_state() === 'free'; } public function get_email_address() { $field = self::get_prefixed_name( 'email_address' ); return get_option( $field ); } public function after_compress_callback( $compressor ) { $count = $compressor->get_compression_count(); if ( ! is_null( $count ) ) { $field = self::get_prefixed_name( 'status' ); update_option( $field, $count ); } $remaining_credits = $compressor->get_remaining_credits(); if ( ! is_null( $remaining_credits ) ) { $field = self::get_prefixed_name( 'remaining_credits' ); update_option( $field, $remaining_credits ); } $paying_state = $compressor->get_paying_state(); if ( ! is_null( $paying_state ) ) { $field = self::get_prefixed_name( 'paying_state' ); update_option( $field, $paying_state ); } $email_address = $compressor->get_email_address(); if ( ! is_null( $email_address ) ) { $field = self::get_prefixed_name( 'email_address' ); update_option( $field, $email_address ); } if ( $compressor->limit_reached() ) { $this->notices->add_limit_reached_notice( $email_address ); } else { $this->notices->remove( 'limit-reached' ); } } public function render_account_status() { $key = $this->get_api_key(); if ( ! empty( $key ) ) { $status = $this->compressor->get_status(); $status->pending = false; if ( $status->ok ) { if ( $this->get_api_key_pending() ) { $this->clear_api_key_pending(); } } elseif ( $this->get_api_key_pending() ) { $status->ok = true; $status->pending = true; } include __DIR__ . '/views/account-status-connected.php'; } } /** * Renders the form to create a Tinify account or to connect one with an API key. */ public function render_account_connect() { $can_create_key = $this->get_compressor()->can_create_key(); $user = wp_get_current_user(); $name = trim( $user->user_firstname . ' ' . $user->user_lastname ); $email = trim( $user->user_email ); $has_api_key = (bool) $this->get_api_key(); include __DIR__ . '/views/account-status-connect.php'; } /** * Renders the switch for WebP or AVIF versions, with the format and the delivery to use. * Server rules are recommended, so they are selected when Apache can rewrite and * no delivery was chosen yet. */ public function render_modern_formats() { $convert_name = self::get_prefixed_name( 'convert_format' ); $convert = $this->get_conversion_enabled(); $convert_to = self::get_convert_format_option( 'convert_to', 'smallest' ); $server_rules = self::can_render_delivery_method(); $delivery = self::get_convert_format_option( 'delivery_method', $server_rules ? 'htaccess' : 'picture' ); include __DIR__ . '/views/settings-modern-formats.php'; } public function render_pending_status() { $key = $this->get_api_key(); if ( ! empty( $key ) ) { include __DIR__ . '/views/account-status-loading.php'; } } public function create_api_key() { check_ajax_referer( 'tiny-compress', '_nonce' ); $compressor = $this->get_compressor(); if ( ! current_user_can( 'manage_options' ) ) { $status = (object) array( 'ok' => false, 'message' => 'This feature requires certain user capabilities', ); } elseif ( $compressor->can_create_key() ) { if ( empty( $_POST['name'] ) ) { $status = (object) array( 'ok' => false, 'message' => __( 'Please enter your name', 'tiny-compress-images' ), ); echo json_encode( $status ); exit(); } if ( empty( $_POST['email'] ) ) { $status = (object) array( 'ok' => false, 'message' => __( 'Please enter your email address', 'tiny-compress-images' ), ); echo json_encode( $status ); exit(); } try { $name = sanitize_text_field( wp_unslash( $_POST['name'] ) ); $email = sanitize_email( wp_unslash( $_POST['email'] ) ); $site = str_replace( array( 'http://', 'https://' ), '', get_bloginfo( 'url' ) ); $identifier = 'WordPress plugin for ' . $site; $link = $this->get_absolute_url(); $options = array( 'name' => $name, 'identifier' => $identifier, ); $compressor->create_key( $email, $options ); update_option( self::get_prefixed_name( 'api_key_pending' ), true ); update_option( self::get_prefixed_name( 'api_key' ), $compressor->get_key() ); update_option( self::get_prefixed_name( 'status' ), 0 ); update_option( self::get_prefixed_name( 'email_address' ), $email ); $status = (object) array( 'ok' => true, 'message' => null, ); } catch ( Tiny_Exception $err ) { list($message) = explode( ' (HTTP', $err->getMessage(), 2 ); $status = (object) array( 'ok' => false, 'message' => $message, ); } } else { $status = (object) array( 'ok' => false, 'message' => 'This feature is not available on your platform', ); } // End if(). echo json_encode( $status ); exit(); } public function update_api_key() { check_ajax_referer( 'tiny-compress', '_nonce' ); $key = null; if ( ! current_user_can( 'manage_options' ) ) { $status = (object) array( 'ok' => false, 'message' => 'This feature requires certain user capabilities', ); } elseif ( empty( $_POST['key'] ) ) { /* Always save if key is blank, so the key can be deleted. */ $status = (object) array( 'ok' => true, 'message' => null, ); } else { $key = sanitize_text_field( wp_unslash( $_POST['key'] ) ); $status = Tiny_Compress::create( $key )->get_status(); } if ( $status->ok ) { update_option( self::get_prefixed_name( 'api_key_pending' ), false ); update_option( self::get_prefixed_name( 'api_key' ), $key ); } echo json_encode( $status ); exit(); } public static function wr2x_active() { return function_exists( 'wr2x_get_retina' ); } public function get_wr2x_option() { $setting = get_option( self::get_prefixed_name( 'sizes' ) ); return array( 'width' => null, 'height' => null, 'tinify' => ( isset( $setting['wr2x'] ) && 'on' === $setting['wr2x'] ), ); } /** * Wether or not wr2x is active * * @return bool true if active */ public function compress_wr2x_images() { $option = $this->get_wr2x_option(); return self::wr2x_active() && $option['tinify']; } /** * @return bool true if apache with mod_rewrite loaded */ public static function can_render_delivery_method() { global $is_apache; if ( ! $is_apache ) { return false; } if ( ! function_exists( 'apache_mod_loaded' ) || ! function_exists( 'apache_get_modules' ) ) { return false; } $modules = apache_get_modules(); return in_array( 'mod_rewrite', $modules, true ); } private static function get_convert_format_option( $option, $default_value ) { $setting = get_option( self::get_prefixed_name( 'convert_format' ) ); if ( isset( $setting[ $option ] ) && $setting[ $option ] ) { return $setting[ $option ]; } return $default_value; } }