Plugin Directory

Changeset 2310548


Ignore:
Timestamp:
05/23/2020 02:01:56 AM (6 years ago)
Author:
pinecast
Message:

[1.0.5] Stop downloading images too many times

Location:
pinecast-wp-sync
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • pinecast-wp-sync/tags/1.0.5/trunk/includes/ajax-functions.php

    r2264272 r2310548  
    1818    if ( $podcast = get_podcast( wp_unslash($_POST['podcast']) ) ) {
    1919        $result = $podcast->sync_episodes();
    20         if (!$result) {
     20        if ($result) {
    2121            wp_send_json( array(
    2222                'success' => false,
    2323                'error' => 'could not sync',
     24                'result' => $result,
     25                'feed_url' => $podcast->get_feed_url(),
    2426            ) );
    2527            return;
  • pinecast-wp-sync/tags/1.0.5/trunk/includes/class-podcast.php

    r2264272 r2310548  
    187187        $response = wp_remote_get(
    188188            $this->feed_url,
    189             ['user-agent' => 'WordPress']
     189            ['user-agent' => 'WordPress; Pinecast Sync Plugin']
    190190        );
    191191        $body     = wp_remote_retrieve_body( $response );
     
    293293     * Synchronize the podcast episodes, create new posts if they don't exist yet.
    294294     *
    295      * @return int[]|bool List of post IDs created on success (can be empty), false on failure.
     295     * @return string|bool Returns an error or false
    296296     */
    297297    public function sync_episodes() {
     
    299299
    300300        if ( empty( $feed->items ) ) {
    301             return false;
     301            return 'Feed was empty';
    302302        }
    303303
     
    315315        }
    316316
    317         return $created_posts;
     317        return false;
    318318    }
    319319
     
    337337            'meta_input'    => array(
    338338                '_pinecast_id' => $data->id,
    339                 '_pinecast_image' => $data->image,
    340339                '_pinecast_feed' => $this->feed_url,
    341340            ),
     
    362361        }
    363362
    364         if ( $this->use_featured_image() && ! empty( $data->image ) ) {
    365             if ( $attachment_id = $this->import_episode_image( $data->image, $post_id ) ) {
     363        if ( $this->use_featured_image() && !empty( $data->image ) ) {
     364            if ( $attachment_id = $this->import_episode_image( $data->image, $post_id, true ) ) {
    366365                set_post_thumbnail( $post_id, $attachment_id );
    367366            }
     
    397396            if (has_post_thumbnail($post_id)) {
    398397                delete_post_thumbnail($post_id);
    399                 delete_metadata('post', $post_id, '_pinecast_image');
     398                delete_post_metadata($post_id, '_pinecast_image');
    400399            }
    401400        }
     
    416415     * @return bool|int Attachment ID on success, false otherwise.
    417416     */
    418     private function import_episode_image( $image_url, $post_id ) {
     417    private function import_episode_image( $image_url, $post_id, $override = false ) {
    419418        require_once ABSPATH . 'wp-includes/post.php';
    420419        require_once ABSPATH . 'wp-admin/includes/image.php';
    421420        require_once ABSPATH . 'wp-admin/includes/file.php';
    422421        require_once ABSPATH . 'wp-admin/includes/media.php';
     422
     423        if (!$override) {
     424            $existing_image = get_post_meta($post_id, '_pinecast_image', true);
     425            if ($existing_image === $image_url) {
     426                // Don't update if it hasn't changed.
     427                return false;
     428            } elseif ($existing_image) {
     429                // If there was an existing image that's not the current image, delete it.
     430                delete_post_thumbnail($post_id);
     431                delete_post_metadata($post_id, '_pinecast_image');
     432            } elseif (has_post_thumbnail($post_id)) {
     433                // If there was an existing image that's not from Pinecast,
     434                // don't overwrite it.
     435                return false;
     436            }
     437        }
    423438
    424439        // Download file to temp dir
     
    437452            $attachment_id = media_handle_sideload( $file, $post_id );
    438453
     454            update_post_meta($post_id, '_pinecast_image', $image_url);
     455
    439456            return $attachment_id;
    440457        }
  • pinecast-wp-sync/trunk/includes/ajax-functions.php

    r2264272 r2310548  
    1818    if ( $podcast = get_podcast( wp_unslash($_POST['podcast']) ) ) {
    1919        $result = $podcast->sync_episodes();
    20         if (!$result) {
     20        if ($result) {
    2121            wp_send_json( array(
    2222                'success' => false,
    2323                'error' => 'could not sync',
     24                'result' => $result,
     25                'feed_url' => $podcast->get_feed_url(),
    2426            ) );
    2527            return;
  • pinecast-wp-sync/trunk/includes/class-podcast.php

    r2264272 r2310548  
    187187        $response = wp_remote_get(
    188188            $this->feed_url,
    189             ['user-agent' => 'WordPress']
     189            ['user-agent' => 'WordPress; Pinecast Sync Plugin']
    190190        );
    191191        $body     = wp_remote_retrieve_body( $response );
     
    293293     * Synchronize the podcast episodes, create new posts if they don't exist yet.
    294294     *
    295      * @return int[]|bool List of post IDs created on success (can be empty), false on failure.
     295     * @return string|bool Returns an error or false
    296296     */
    297297    public function sync_episodes() {
     
    299299
    300300        if ( empty( $feed->items ) ) {
    301             return false;
     301            return 'Feed was empty';
    302302        }
    303303
     
    315315        }
    316316
    317         return $created_posts;
     317        return false;
    318318    }
    319319
     
    337337            'meta_input'    => array(
    338338                '_pinecast_id' => $data->id,
    339                 '_pinecast_image' => $data->image,
    340339                '_pinecast_feed' => $this->feed_url,
    341340            ),
     
    362361        }
    363362
    364         if ( $this->use_featured_image() && ! empty( $data->image ) ) {
    365             if ( $attachment_id = $this->import_episode_image( $data->image, $post_id ) ) {
     363        if ( $this->use_featured_image() && !empty( $data->image ) ) {
     364            if ( $attachment_id = $this->import_episode_image( $data->image, $post_id, true ) ) {
    366365                set_post_thumbnail( $post_id, $attachment_id );
    367366            }
     
    397396            if (has_post_thumbnail($post_id)) {
    398397                delete_post_thumbnail($post_id);
    399                 delete_metadata('post', $post_id, '_pinecast_image');
     398                delete_post_metadata($post_id, '_pinecast_image');
    400399            }
    401400        }
     
    416415     * @return bool|int Attachment ID on success, false otherwise.
    417416     */
    418     private function import_episode_image( $image_url, $post_id ) {
     417    private function import_episode_image( $image_url, $post_id, $override = false ) {
    419418        require_once ABSPATH . 'wp-includes/post.php';
    420419        require_once ABSPATH . 'wp-admin/includes/image.php';
    421420        require_once ABSPATH . 'wp-admin/includes/file.php';
    422421        require_once ABSPATH . 'wp-admin/includes/media.php';
     422
     423        if (!$override) {
     424            $existing_image = get_post_meta($post_id, '_pinecast_image', true);
     425            if ($existing_image === $image_url) {
     426                // Don't update if it hasn't changed.
     427                return false;
     428            } elseif ($existing_image) {
     429                // If there was an existing image that's not the current image, delete it.
     430                delete_post_thumbnail($post_id);
     431                delete_post_metadata($post_id, '_pinecast_image');
     432            } elseif (has_post_thumbnail($post_id)) {
     433                // If there was an existing image that's not from Pinecast,
     434                // don't overwrite it.
     435                return false;
     436            }
     437        }
    423438
    424439        // Download file to temp dir
     
    437452            $attachment_id = media_handle_sideload( $file, $post_id );
    438453
     454            update_post_meta($post_id, '_pinecast_image', $image_url);
     455
    439456            return $attachment_id;
    440457        }
Note: See TracChangeset for help on using the changeset viewer.