From e4fdea6773dd641350e6bc6fa90aafe1adaaf10d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Tue, 7 Apr 2020 17:28:17 +0000 Subject: [PATCH 1/4] Fix almost all PHPStan Level 0 errors --- .../instance.php | 18 ++++++++---------- .../php/class-connect.php | 4 ++-- .../php/class-media.php | 7 +++---- .../php/class-plugin.php | 3 +-- .../php/class-settings-page.php | 2 +- .../php/class-sync.php | 2 +- .../php/connect/class-api.php | 12 +++++++----- .../php/media/class-global-transformations.php | 4 ++-- .../php/sync/class-download-sync.php | 2 +- .../php/sync/class-push-sync.php | 17 +++++++++-------- 10 files changed, 35 insertions(+), 36 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php index bc0c3edda..e09a39c2d 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php @@ -7,25 +7,23 @@ namespace Cloudinary; -global $cloudinary_plugin; - -if ( ! defined( 'DEBUG_SCRIPTS' ) ) { - define( 'CLDN_ASSET_DEBUG', '.min' ); -} else { - define( 'CLDN_ASSET_DEBUG', '' ); -} +define( 'CLDN_ASSET_DEBUG', defined( 'DEBUG_SCRIPTS' ) ? '' : '.min' ); +require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once __DIR__ . '/php/class-plugin.php'; -$cloudinary_plugin = new Plugin(); - /** * Cloudinary Plugin Instance * * @return Plugin */ function get_plugin_instance() { - global $cloudinary_plugin; + static $cloudinary_plugin; + if ( ! isset( $cloudinary_plugin ) ) { + $cloudinary_plugin = new Plugin(); + } return $cloudinary_plugin; } + +get_plugin_instance(); diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-connect.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-connect.php index 7f2b62d39..2483ffdb1 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-connect.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-connect.php @@ -10,7 +10,7 @@ use Cloudinary\Component\Config; use Cloudinary\Component\Notice; use Cloudinary\Component\Setup; -use Cloudinary\Connect\API; +use Cloudinary\Connect\Api; /** * Cloudinary connection class. @@ -33,7 +33,7 @@ class Connect implements Config, Setup, Notice { * * @since 0.1 * - * @var \Cloudinary\Api + * @var \Cloudinary\Connect\Api */ public $api; diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index cd966dc35..fe1cbee86 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -368,7 +368,7 @@ public function set_transformation( &$transformations, $type, $value, $index = f * @param array $transformations The transformation set to check. * @param string $type The type of transformation to check for. * - * @return bool + * @return int|false */ public function get_transformation( $transformations, $type ) { foreach ( $transformations as $index => $transformation ) { @@ -549,7 +549,7 @@ public function apply_default_transformations( array $transformations, $type = ' * @param bool $breakpoint Flag url is a breakpoint URL to stop re-applying default transformations. * @param bool $clean Flag to present a clean url (With out a WP size variable. * - * @return string The converted URL. + * @return string|false The converted URL. */ public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $breakpoint = false, $clean = false ) { @@ -706,9 +706,8 @@ public function cloudinary_id( $attachment_id ) { * @param int $attachment_id The ID of the attachment. * @param string|array $size The requested size of the image. * - * @return array The image array of size and url. + * @return array|null The image array of size and url. * @uses filter:image_downsize - * */ public function filter_downsize( $image, $attachment_id, $size ) { // Don't do this while saving. diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-plugin.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-plugin.php index 2f07275ba..bb94526a0 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-plugin.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-plugin.php @@ -95,7 +95,6 @@ class Plugin { * Plugin_Base constructor. */ public function __construct() { - require_once ABSPATH . 'wp-admin/includes/plugin.php'; $plugin = get_plugin_data( CLDN_CORE ); $location = $this->locate_plugin(); $this->slug = ! empty( $plugin['TextDomain'] ) ? $plugin['TextDomain'] : $location['dir_basename']; @@ -288,7 +287,7 @@ public function setup() { */ public function admin_notices() { /** - * @var $components Notice[] + * @var Notice[] $components * * An array of classes that implement the Notice interface. */ diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php index 237c61557..5e7bb2c4b 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php @@ -175,7 +175,7 @@ private function register_section( $tab_slug ) { * * @since 0.1 * - * @param string $tab The tab to register the section fields for. + * @param array $tab The tab to register the section fields for. * @param string $setting_slug The slug of the setting to register section for. */ private function register_section_fields( $tab, $setting_slug ) { diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php index a2bcd3745..5912509fc 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php @@ -113,7 +113,7 @@ public function is_synced( $post_id ) { * * @param int $post_id The post id to generate a signature for. * - * @return string + * @return array */ public function generate_signature( $post_id ) { $upload = $this->managers['push']->prepare_upload( $post_id ); diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php index bad5c3d94..3903a9dc0 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php @@ -129,13 +129,13 @@ class Api { * * @var string|null */ - private $pending_url = array(); + private $pending_url = null; /** * API constructor. * * @param \Cloudinary\Connect $connect The connect object. - * @param string The plugin version. + * @param string $version The plugin version. */ public function __construct( $connect, $version ) { $this->credentials = $connect->get_credentials(); @@ -294,6 +294,7 @@ public function upload_large( $file, $args ) { $chunk_size = 20000000; $index = 0; $file_size = filesize( $file ); + $result = array(); while ( ! feof( $src ) ) { $current_loc = $index * $chunk_size; if ( $current_loc >= $file_size ) { @@ -325,7 +326,6 @@ public function upload_large( $file, $args ) { } return $result; - } /** @@ -496,7 +496,7 @@ public function __call( $name, $args ) { * * @param array $args Array of parameters to sign. * - * @return array|\WP_Error + * @return string */ public function sign( $args ) { @@ -545,7 +545,7 @@ public function set_data( $handle, $request, $url ) { * @param array $args The optional arguments to send. * @param string $method The call HTTP method. * - * @return array|\WP_Error + * @return array|string|\WP_Error */ private function call( $url, $args = array(), $method = 'get' ) { $args['method'] = strtoupper( $method ); @@ -572,11 +572,13 @@ private function call( $url, $args = array(), $method = 'get' ) { if ( is_wp_error( $request ) ) { return $request; } + $body = wp_remote_retrieve_body( $request ); $result = json_decode( $body, ARRAY_A ); if ( empty( $result ) && ! empty( $body ) ) { return $body; // not json. } + if ( ! empty( $result['error'] ) && ! empty( $result['error']['message'] ) ) { return new \WP_Error( $request['response']['code'], $result['error']['message'] ); } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php index fa07794ce..3948826c5 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php @@ -197,7 +197,7 @@ function ( $value ) { * * @param string $type The type to get. * - * @return array + * @return string */ public function get_taxonomy_transformations( $type ) { $return_transformations = ''; @@ -265,7 +265,7 @@ public function taxonomy_ordering( $type, $post ) { /** * Check if the post has any public taxonomies. * - * @param \WP_POST $post The post to check. + * @param \WP_Post $post The post to check. * * @return bool */ diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php index cb4c9e6c6..6cca66fb7 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php @@ -117,7 +117,7 @@ public function rest_download_asset( \WP_REST_Request $request ) { /** * Prepare and sync down an asset stored remotely. * - * @param $attachment_id + * @param string $attachment_id * * @return array|\WP_Error */ diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index f69020e31..b708381da 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -273,7 +273,7 @@ public function get_resource_type( $attachment ) { * * @param int|\WP_Post $attachment The attachment to get the required sync type for. * - * @return string + * @return string|\WP_Error */ private function get_sync_type( $attachment ) { if ( is_numeric( $attachment ) ) { @@ -333,7 +333,10 @@ public function prepare_upload( $post, $down_sync = false ) { $file = get_attached_file( $post->ID ); if ( empty( $file ) ) { return new \WP_Error( 'attachment_no_file', __( 'Attachment did not have a file.', 'cloudinary' ) ); - } elseif ( ! file_exists( $file ) ) { + } + + if ( ! file_exists( $file ) ) { + $file_size = 0; // May be an old upload type. $src = get_post_meta( $post->ID, '_wp_attached_file', true ); if ( $this->plugin->components['media']->is_cloudinary_url( $src ) ) { @@ -347,8 +350,6 @@ public function prepare_upload( $post, $down_sync = false ) { } $file = get_attached_file( $post->ID ); $file_size = filesize( $file ); - } else { - $file_size = 0; } } } else { @@ -394,7 +395,7 @@ public function prepare_upload( $post, $down_sync = false ) { 'public_id' => $public_id, 'context' => array( 'caption' => esc_attr( $post->post_title ), - 'alt' => $post->_wp_attachment_image_alt, + 'alt' => get_post_meta( $post->ID, '_wp_attachment_image_alt', true ), ), ); @@ -439,9 +440,9 @@ public function prepare_upload( $post, $down_sync = false ) { /** * Filter the options to allow other plugins to add requested options for uploading. * - * @param array $options The options array. - * @param \WP_Post $post The attachment post. - * @param \Cloudinary\Sync The sync object instance. + * @param array $options The options array. + * @param \WP_Post $post The attachment post. + * @param \Cloudinary\Sync $instance The sync object instance. * * @return array */ From 70b11308865d59fd3926802b67225a721971c1ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Fri, 29 May 2020 14:27:32 +0000 Subject: [PATCH 2/4] Fix all WordPress VIp requirements ```bash vendor/bin/phpcs --standard=WordPress-VIP-Go --exclude=WordPress.WP.TimezoneChange \ cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/ cloudinary-image-management-and-manipulation-in-the-cloud-cdn/cloudinary.php ``` as per https://github.com/Automattic/VIP-Coding-Standards/issues/457 --- .../php/class-settings-page.php | 2 +- .../php/media/class-global-transformations.php | 2 +- .../php/sync/class-delete-sync.php | 2 +- .../php/sync/class-push-sync.php | 2 +- .../php/templates/taxonomy-term-transformation-fields.php | 3 +-- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php index 5e7bb2c4b..6ed21bd4a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-settings-page.php @@ -827,7 +827,7 @@ public function active_tab() { } $tab = filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_STRING ); if ( ! $this->validate_tab( $tab ) ) { // Tab is invalid or not set, check if in a POST. - $tab = filter_input( INPUT_POST, 'tab', FILTER_DEFAULT ); + $tab = filter_input( INPUT_POST, 'tab', FILTER_SANITIZE_STRING ); if ( ! $this->validate_tab( $tab ) ) { // Tab is invalid or not set, load the default/first tab. $tab = array_keys( $page['tabs'] ); $tab = array_shift( $tab ); diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php index 3948826c5..b5e0a0621 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-global-transformations.php @@ -170,7 +170,7 @@ public function get_transformations( $type ) { // check screen context. switch ( $screen->base ) { case 'term': - $term_id = filter_input( INPUT_GET, 'tag_ID', FILTER_DEFAULT ); + $term_id = filter_input( INPUT_GET, 'tag_ID', FILTER_SANITIZE_STRING ); $transformations = $this->get_term_transformations( $term_id, $type ); break; case 'toplevel_page_cloudinary': diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-delete-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-delete-sync.php index 3852f38a4..5471cb8cc 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-delete-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-delete-sync.php @@ -63,7 +63,7 @@ public function can_delete_asset( $all_caps, $caps, $args ) { $has_error = $this->plugin->components['media']->get_post_meta( $post_id, Sync::META_KEYS['sync_error'], true ); if ( empty( $has_error ) ) { $all_caps['delete_posts'] = false; - if ( filter_input( INPUT_GET, 'action', FILTER_DEFAULT ) ) { + if ( filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING ) ) { wp_die( esc_html__( 'Sorry, you can’t delete an asset until it has fully synced with Cloudinary. Try again once syncing is complete.', 'cloudinary' ) ); } } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index b708381da..d64426e47 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -602,7 +602,7 @@ public function push_attachments( $attachments ) { wp_update_attachment_metadata( $attachment->ID, $meta ); $this->plugin->components['media']->update_post_meta( $attachment->ID, Sync::META_KEYS['public_id'], $upload['options']['public_id'] ); // Search and update link references in content. - $content_search = new \WP_Query( array( 's' => 'wp-image-' . $attachment->ID, 'fields' => 'ids', 'posts_per_page' => 1000 ) ); + $content_search = new \WP_Query( array( 's' => 'wp-image-' . $attachment->ID, 'fields' => 'ids', 'posts_per_page' => 100 ) ); if ( ! empty( $content_search->found_posts ) ) { $content_posts = array_unique( $content_search->get_posts() ); // ensure post only gets updated once. foreach ( $content_posts as $content_id ) { diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/templates/taxonomy-term-transformation-fields.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/templates/taxonomy-term-transformation-fields.php index 35c52c1dd..d346e942f 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/templates/taxonomy-term-transformation-fields.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/templates/taxonomy-term-transformation-fields.php @@ -35,5 +35,4 @@ ?> - - + Date: Fri, 29 May 2020 14:32:44 +0000 Subject: [PATCH 3/4] Fix static var checking in instance.php --- .../instance.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php index e09a39c2d..c70f3cd2a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php @@ -20,7 +20,7 @@ function get_plugin_instance() { static $cloudinary_plugin; - if ( ! isset( $cloudinary_plugin ) ) { + if ( ! $cloudinary_plugin instanceof Plugin ) { $cloudinary_plugin = new Plugin(); } return $cloudinary_plugin; From 796036a11f07f810d82623bacccf06f87bec90a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Fri, 29 May 2020 14:37:51 +0000 Subject: [PATCH 4/4] Remove `require` from previous PR --- .../instance.php | 1 - 1 file changed, 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php index c70f3cd2a..01f841524 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/instance.php @@ -9,7 +9,6 @@ define( 'CLDN_ASSET_DEBUG', defined( 'DEBUG_SCRIPTS' ) ? '' : '.min' ); -require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once __DIR__ . '/php/class-plugin.php'; /**