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 .= '