d, 'content-length' ); $image['original_size'] = (int) wp_remote_retrieve_header( $original, 'content-length' ); return $this->response( $image ); } /** * Crawl & upload initial load. * * @return WP_REST_Response If there are more posts left to receive. */ public function upload_onboard_images( WP_REST_Request $request ) { $offset = absint( $request->get_param( 'offset' ) ); // Arguments for get_posts function $args = [ 'post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => 100, 'offset' => $offset, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'orderby' => [ 'parent' => 'DESC', ], ]; // Initialize an array to store the image URLs $image_urls = []; add_filter( 'optml_dont_replace_url', '__return_true' ); // If site has a logo, get the logo url if offset is 0, ie first request if ( $offset === 0 ) { $custom_logo_id = get_theme_mod( 'custom_logo' ); if ( $custom_logo_id ) { $image_urls[] = wp_get_attachment_url( $custom_logo_id ); } } $query = new \WP_Query( $args ); if ( $query->have_posts() ) { $image_ids = $query->get_posts(); $image_urls = array_map( 'wp_get_attachment_url', $image_ids ); } remove_filter( 'optml_dont_replace_url', '__return_true' ); if ( count( $image_urls ) === 0 ) { return $this->response( true ); } $api = new Optml_Api(); $api->call_onboard_api( $image_urls ); // Check if image_url array has at least 100 items, if not, we have reached the end of the images return $this->response( count( $image_urls ) < 100 ? true : false ); } /** * Return sample image data. * * @return array Image data. */ private function fetch_sample_image() { $accepted_mimes = [ 'image/jpeg' ]; $args = [ 'post_type' => 'attachment', 'post_status' => 'any', 'number' => '5', 'no_found_rows' => true, 'fields' => 'ids', 'post_mime_type' => $accepted_mimes, 'post_parent__not_in' => [ 0 ], ]; $image_result = new WP_Query( $args ); if ( empty( $image_result->posts ) ) { $rand_id = rand( 1, 3 ); $original_image_url = OPTML_URL . 'assets/img/' . $rand_id . '.jpg'; return [ 'url' => $original_image_url, 'width' => '700', 'height' => '465', 'id' => - 1, ]; } $attachment_id = $image_result->posts[ array_rand( $image_result->posts, 1 ) ]; $original_image_url = wp_get_attachment_image_url( $attachment_id, 'full' ); $metadata = wp_get_attachment_metadata( $attachment_id ); $width = 'auto'; $height = 'auto'; $size = 'full'; if ( isset( $metadata['sizes'] ) && isset( $metadata['sizes'][ $size ] ) ) { $width = $metadata['sizes'][ $size ]['width']; $height = $metadata['sizes'][ $size ]['height']; } return [ 'url' => $original_image_url, 'id' => $attachment_id, 'width' => $width, 'height' => $height, ]; } /** * Disconnect from optimole service. * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @param WP_REST_Request $request disconnect rest request. */ public function disconnect( WP_REST_Request $request ) { $settings = new Optml_Settings(); $settings->reset(); wp_send_json_success( 'Disconnected' ); } /** * Get optimized images from API. * * @param WP_REST_Request $request rest request. * * @return WP_REST_Response */ public function poll_optimized_images( WP_REST_Request $request ) { $api_key = $request->get_param( 'api_key' ); $request = new Optml_Api(); $images = $request->get_optimized_images( $api_key ); if ( ! isset( $images['list'] ) || empty( $images['list'] ) ) { return $this->response( [] ); } $final_images = array_splice( $images['list'], 0, 10 ); foreach ( $final_images as $index => $value ) { $final_images[ $index ]['url'] = Optml_Media_Offload::instance()->get_media_optimized_url( $value['url'], $value['key'], 140, 140, [ 'type' => ResizeTypeProperty::FILL, 'enlarge' => false, 'gravity' => Position::CENTER, ] ); unset( $final_images[ $index ]['key'] ); } return $this->response( $final_images ); } /** * Get watermarks from API. * * @param WP_REST_Request $request rest request. * * @return WP_REST_Response */ public function poll_watermarks( WP_REST_Request $request ) { $api_key = $request->get_param( 'api_key' ); $request = new Optml_Api(); $watermarks = $request->get_watermarks( $api_key ); if ( ! isset( $watermarks['watermarks'] ) || empty( $watermarks['watermarks'] ) ) { return $this->response( [] ); } $final_images = array_splice( $watermarks['watermarks'], 0, 10 ); return $this->response( $final_images ); } /** * Add watermark. * * @param WP_REST_Request $request rest request. * * @return WP_REST_Response */ public function add_watermark( WP_REST_Request $request ) { $file = $request->get_file_params(); $request = new Optml_Api(); $response = $request->add_watermark( $file ); if ( $response === false ) { return $this->response( __( 'Error uploading image. Please try again.', 'optimole-wp' ), 'error' ); } return $this->response( __( 'Watermark image uploaded succesfully !', 'optimole-wp' ) ); } /** * Remove watermark. * * @param WP_REST_Request $request rest request. * * @return WP_REST_Response */ public function remove_watermark( WP_REST_Request $request ) { $post_id = $request->get_param( 'postID' ); $api_key = $request->get_param( 'api_key' ); $request = new Optml_Api(); return $this->response( $request->remove_watermark( $post_id, $api_key ) ); } /** * Get conflicts from API. * * @param WP_REST_Request $request rest request. * * @return WP_REST_Response */ public function poll_conflicts( WP_REST_Request $request ) { $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] ); $manager = new Optml_Conflict_Manager( $conflicts_to_register ); return $this->response( [ 'count' => $manager->get_conflict_count(), 'conflicts' => $manager->get_conflict_list(), ] ); } /** * Dismiss conflict. * * @param WP_REST_Request $request rest request. * * @return WP_REST_Response */ public function dismiss_conflict( WP_REST_Request $request ) { $conflict_id = $request->get_param( 'conflictID' ); $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] ); $manager = new Optml_Conflict_Manager( $conflicts_to_register ); $manager->dismiss_conflict( $conflict_id ); return $this->response( [ 'count' => $manager->get_conflict_count(), 'conflicts' => $manager->get_conflict_list(), ] ); } /** * Request stats update for app. * * @param WP_REST_Request $request rest request. * * @return WP_REST_Response */ public function request_update( WP_REST_Request $request ) { do_action( 'optml_daily_sync' ); $settings = new Optml_Settings(); $service_data = $settings->get( 'service_data' ); if ( empty( $service_data ) ) { return $this->response( '', 'disconnected' ); } return $this->response( $service_data ); } /** * Update options method. * * @param WP_REST_Request $request option update rest request. * * @return WP_REST_Response */ public function update_option( WP_REST_Request $request ) { $new_settings = $request->get_param( 'settings' ); if ( empty( $new_settings ) ) { wp_send_json_error( 'No option key set.' ); } $settings = new Optml_Settings(); $sanitized = $settings->parse_settings( $new_settings ); return $this->response( $sanitized ); } /** * Update options method. * * @param WP_REST_Request $request option update rest request. * * @return WP_REST_Response */ public function check_redirects( WP_REST_Request $request ) { if ( empty( $request->get_param( 'images' ) ) ) { return $this->response( __( 'No images available on the current page.' ), 'noImagesFound' ); } // 'ok' if no issues found, 'log' is there are issues we need to notify, 'deactivated' if the user's account is disabled $status = 'ok'; $result = ''; foreach ( $request->get_param( 'images' ) as $domain => $value ) { $args = [ 'method' => 'GET', 'redirection' => 0, ]; $processed_images = 0; if ( isset( $value['src'] ) ) { $processed_images = count( $value['src'] ); } if ( isset( $value['ignoredUrls'] ) && $value['ignoredUrls'] > $processed_images ) { $result .= '
  • ❌ ' . sprintf( /* translators: 1 is the domain name, 2 is starting anchor tag, 3 is the ending anchor tag. */__( 'The images from: %1$s are not optimized by Optimole. If you would like to do so, you can follow this: %2$sWhy Optimole does not optimize all the images from my site?%3$s.', 'optimole-wp' ), $domain, '', '' ) . '
  • '; $status = 'log'; continue; } if ( $processed_images > 0 ) { $response = wp_remote_get( $value['src'][ rand( 0, $processed_images - 1 ) ], $args ); if ( ! is_wp_error( $response ) && is_array( $response ) ) { $headers = $response['headers']; // array of http header lines $status_code = $response['response']['code']; if ( $status_code === 301 ) { $status = 'deactivated'; $result = '
  • ❌ ' . sprintf( /* translators: 1 is starting anchor tag, 2 is the ending anchor tag. */ __( 'Your account is currently disabled due to exceeding quota and Optimole is no longer able to optimize the images. In order to fix this you will need to %1$supgrade%2$s.', 'optimole-wp' ), '', '' ) . '
  • '; break; } if ( $status_code === 302 ) { if ( isset( $headers['x-redirect-o'] ) ) { $optimole_code = (int) $headers['x-redirect-o']; if ( $optimole_code === 1 ) { $status = 'log'; $result .= '
  • ❌ ' . sprintf( /* translators: 1 is the domain, 2 is starting anchor tag, 3 is the ending anchor tag. */ __( 'The domain: %1$s is not allowed to optimize images using your Optimole account. You can add this to the allowed list %2$shere%3$s.', 'optimole-wp' ), '' . $domain . '', '', '' ) . '
  • '; } if ( $optimole_code === 4 ) { $status = 'log'; $result .= '
  • ❌ ' . sprintf( /* translators: 1 is the domain, 2 is starting anchor tag, 3 is the ending anchor tag. */ __( 'We are not able to download the images from %1$s. Please check %2$sthis%3$s document for a more advanced guide on how to solve this.', 'optimole-wp' ), '' . $domain . '', '', '' ) . '
    ' . '
  • '; } } } } } } if ( $result === '' ) { $result = __( 'No issues detected, everything is running smoothly.', 'optimole-wp' ); } return $this->response( '', $status ); } /** * Get total number of images. * * @param WP_REST_Request $request rest request object. * * @return WP_REST_Response */ public function number_of_images_and_pages( WP_REST_Request $request ) { $action = 'offload_images'; $refresh = false; $images = []; if ( ! empty( $request->get_param( 'action' ) ) ) { $action = $request->get_param( 'action' ); } if ( ! empty( $request->get_param( 'refresh' ) ) ) { $refresh = $request->get_param( 'refresh' ); } if ( ! empty( $request->get_param( 'images' ) ) ) { $images = $request->get_param( 'images' ); } return $this->response( Optml_Media_Offload::get_image_count( $action, $refresh, $images ) ); } /** * Get conflicts list. * * @param WP_REST_Request $request rest request object. * * @return WP_REST_Response */ public function get_offload_conflicts( WP_REST_Request $request ) { $request = new Optml_Api(); $decoded_list = $request->get_offload_conflicts(); $active_conflicts = []; if ( isset( $decoded_list['plugins'] ) ) { foreach ( $decoded_list['plugins'] as $slug ) { if ( is_plugin_active( $slug ) ) { $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $slug ); if ( ! empty( $plugin_data['Name'] ) ) { $active_conflicts[] = $plugin_data['Name']; } } } } return $this->response( $active_conflicts ); } /** * Insert images request. * * @param WP_REST_Request $request insert images rest request. * * @return WP_REST_Response */ public function insert_images( WP_REST_Request $request ) { $images = $request->get_param( 'images' ); if ( ! is_array( $images ) || empty( $images ) ) { return $this->response( [ 'error' => 'No images found' ] ); } $insert = Optml_Main::instance()->dam->insert_attachments( $images ); return $this->response( $insert ); } }