Skip to content

Fixes from phpstan #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@

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 __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 ( ! $cloudinary_plugin instanceof Plugin ) {
$cloudinary_plugin = new Plugin();
}
return $cloudinary_plugin;
}

get_plugin_instance();
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,7 +33,7 @@ class Connect implements Config, Setup, Notice {
*
* @since 0.1
*
* @var \Cloudinary\Api
* @var \Cloudinary\Connect\Api
*/
public $api;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -325,7 +326,6 @@ public function upload_large( $file, $args ) {
}

return $result;

}

/**
Expand Down Expand Up @@ -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 ) {

Expand Down Expand Up @@ -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 );
Expand All @@ -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'] );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 ),
),
);

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -601,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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@
?>
</td>
</tr>
<?php endforeach; ?>

<?php endforeach;