Plugin Directory

Changeset 2470907


Ignore:
Timestamp:
02/08/2021 03:21:37 PM (5 years ago)
Author:
hellowoofy10
Message:

added possibility upload featured image

Location:
hellowoofy-com
Files:
17 added
1 edited

Legend:

Unmodified
Added
Removed
  • hellowoofy-com/trunk/hellowoofy-com.php

    r2445216 r2470907  
    44 * Plugin URI: https://hellowoofy.com/hellowoofy-wordpress-plugin/
    55 * Description: Create marketing content automatically using data science.
    6  * Version: 1.0.1
     6 * Version: 1.0.2
    77 * Author: HelloWoofy.com
    88 * Author URI: https://hellowoofy.com
     
    3030 */
    3131
    32 if (!defined('ABSPATH')) {
    33     die;
    34 }
    35 
    3632class WoofyIntegration
    3733{
     
    4743    }
    4844
    49 
     45    /**
     46     * Plugin settings
     47     */
    5048    public function woofy_settings()
    5149    {
     
    7472    }
    7573
     74    /**
     75     *  Route for creating posts
     76     */
    7677    public function woofy_register_route()
    7778    {
     
    127128    }
    128129
     130    /**
     131     * @param $url
     132     * @param null $post_id
     133     * @return string|WP_Error
     134     * Function for attaching feature image to post
     135     */
     136    private function woofy_insert_attachment($image_url, $post_id = null, $user_id) {
     137        if ( ! function_exists( 'media_handle_upload' )
     138           && user_can($user_id, 'edit_posts'))
     139        {
     140            require_once ABSPATH . 'wp-admin/includes/media.php';
     141            require_once ABSPATH . 'wp-admin/includes/file.php';
     142            require_once ABSPATH . 'wp-admin/includes/image.php';
     143
     144            $img_id = media_sideload_image($image_url, $post_id, null, 'id');
     145            set_post_thumbnail($post_id, $img_id);
     146
     147            if( is_wp_error($img_id) ){
     148               return new WP_Error( 400, __( "Image wasn't uploaded", "woofy" ) );
     149            }
     150            return wp_get_attachment_image_src($img_id);
     151        } else {
     152            return new WP_Error( 400, __( "Check 'media_handle_upload' failed", "woofy" ) );
     153        }
     154    }
     155
    129156    public function woofy_set_cat_func()
    130157    {
    131158        $args_for_send = array('category', 'post_tag');
     159        // Added to DB
    132160        $categories = get_terms($args_for_send, 'orderby=name&hide_empty=0');
    133161        $categories_for_send = [];
     
    156184        $key_decode = base64_decode($unique_user_key);
    157185        $user_id = stristr($key_decode, '=', true);
     186        $image_url = $request->get_param('journalPostFeaturedImage');
    158187
    159188        // create array for Post
     
    184213        // Added to DB
    185214        $post_id = wp_insert_post(wp_slash($post_data));
     215
     216        if( is_wp_error($post_id) ){
     217            return $post_id->get_error_message();
     218        }
     219
    186220        wp_set_object_terms($post_id, $post_category, 'category');
    187221        wp_set_object_terms($post_id, $post_tags, 'post_tag');
     222
     223        if ($image_url) {
     224            $thumbnail = $this->woofy_insert_attachment($image_url, $post_id, $user_id);
     225        }else {
     226            $thumbnail = "Image wasn't set in request";
     227        }
     228
     229        if (is_wp_error($thumbnail)) {
     230            return $thumbnail->get_error_messages();
     231        }
    188232
    189233        return $result = array(
     
    191235            'post_id' => $post_id,
    192236            'post_url' => get_permalink($post_id),
     237            'thumbnail' => $thumbnail ? $thumbnail : ''
    193238        );
    194239    }
Note: See TracChangeset for help on using the changeset viewer.