Plugin Directory

Changeset 2885241


Ignore:
Timestamp:
03/22/2023 09:40:29 PM (3 years ago)
Author:
alphatrait
Message:

Big Fixed

Location:
contentify-ai
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • contentify-ai/trunk/index.php

    r2881532 r2885241  
    22/*
    33Plugin Name: Contentify AI
    4 Description: Allows Co-Writer to write blogs and optimize the posts.
    5 Version: 1.0.0
     4Description: This plugin is a Contentify Editor AI (Co-Editor). It publishes and optimize the content that Contentify Writer AI (Co-Writer) generates.
     5Version: 1.1.4
    66Author: Contentify Team
    77Text Domain: contentify-ai
    88*/
    99
    10 defined( 'ABSPATH' ) or die;
    11 
    12 define( 'CONTENTIFY_AI_FILE', __FILE__ );
    13 define( 'CONTENTIFY_AI_VER', '1.0.0' );
    14 
    15 if ( ! class_exists( 'Contentify_AI_Class' ) ) {
    16     class Contentify_AI_Class {
    17         public static function get_instance() {
    18             if ( self::$instance == null ) {
     10defined('ABSPATH') or die;
     11
     12define('CONTENTIFY_AI_FILE', __FILE__);
     13define('CONTENTIFY_AI_VER', '1.0.0');
     14
     15if (!class_exists('Contentify_AI_Class')) {
     16    class Contentify_AI_Class
     17    {
     18        public static function get_instance()
     19        {
     20            if (self::$instance == null) {
    1921                self::$instance = new self();
    2022            }
     
    2426        private static $instance = null;
    2527
    26         private function __clone() { }
    27 
    28         public function __wakeup() { }
    29 
    30         private function __construct() {
     28        private function __clone()
     29        {
     30        }
     31
     32        public function __wakeup()
     33        {
     34        }
     35
     36        private function __construct()
     37        {
    3138            // Init
    32             add_action( 'init', array( $this, 'init' ) );
     39            add_action('init', array($this, 'init'));
    3340
    3441            // REST API Routes
    35             add_action( 'rest_api_init', array( $this, 'create_api_key_authentication' ) );
    36             add_action( 'rest_api_init', array( $this, 'create_get_website_title_endpoint' ) );
     42            add_action('rest_api_init', array($this, 'create_api_key_authentication'));
     43            add_action('rest_api_init', array($this, 'create_get_website_title_endpoint'));
    3744
    3845            // Register the plugin's settings page
    39             add_action( 'admin_menu', array( $this, 'contentify_settings' ) );
     46            add_action('admin_menu', array($this, 'contentify_settings'));
    4047
    4148            // Register the plugin's settings
    42             add_action( 'admin_init', array( $this, 'register_contentify_settings' ) );
    43         }
    44 
    45         public function init() {
    46             load_plugin_textdomain( 'contentify-ai', false, dirname( plugin_basename( CONTENTIFY_AI_FILE ) ) . '/languages' );
    47         }
    48 
    49         public function create_api_key_authentication() {
    50             register_rest_route( 'contentify/v1', '/create-post/', array(
     49            add_action('admin_init', array($this, 'register_contentify_settings'));
     50        }
     51
     52        public function init()
     53        {
     54            load_plugin_textdomain('contentify-ai', false, dirname(plugin_basename(CONTENTIFY_AI_FILE)) . '/languages');
     55        }
     56
     57        public function create_api_key_authentication()
     58        {
     59            register_rest_route('contentify/v1', '/create-post/', array(
    5160                'methods' => 'POST',
    52                 'callback' => array( $this, 'create_post_with_api_key' ),
     61                'callback' => array($this, 'create_post_with_api_key'),
    5362                'permission_callback' => '__return_true',
    54             ) );
    55         }
    56 
    57         public function create_get_website_title_endpoint() {
    58             register_rest_route( 'contentify/v1', '/website-title/', array(
     63            )
     64            );
     65        }
     66
     67        public function create_get_website_title_endpoint()
     68        {
     69            register_rest_route('contentify/v1', '/website-title/', array(
    5970                'methods' => 'POST',
    60                 'callback' => array( $this, 'get_website_title_with_api_key' ),
     71                'callback' => array($this, 'get_website_title_with_api_key'),
    6172                'permission_callback' => '__return_true',
    62             ) );
    63         }
    64 
    65         public function get_website_title_with_api_key( $data ) {
    66             $api_key = $data->get_param( 'api_key' );
     73            )
     74            );
     75        }
     76
     77        public function get_website_title_with_api_key($data)
     78        {
     79            $api_key = $data->get_param('api_key');
    6780
    6881            // Validate the API key
    69             if ( ! $this->validate_api_key( $api_key ) ) {
    70                 return new WP_Error( 'invalid_api_key', __( 'The API key provided is invalid', 'contentify-ai' ), array( 'status' => 401 ) );
    71             }
    72            
     82            if (!$this->validate_api_key($api_key)) {
     83                return new WP_Error('invalid_api_key', __('The API key provided is invalid', 'contentify-ai'), array('status' => 401));
     84            }
     85
    7386            // Use the WordPress function to get the current website title
    74             $title = get_option( 'blogname' );
    75            
     87            $title = get_option('blogname');
     88
    7689            // Return the title
    77             return array( 'website_title' => $title );
    78         }
    79 
    80 
    81         function create_post_with_api_key( $data ) {
    82             $author = $data->get_param( 'author' );
    83             $api_key = $data->get_param( 'api_key' );
    84             $title = $data->get_param( 'title' );
    85             $status = $data->get_param( 'status' );
    86             $content = $data->get_param( 'content' );
    87             $keyword = $data->get_param( 'keyword' );
    88             $seo_title = $data->get_param( 'seo_title' );
    89             $seo_description = $data->get_param( 'seo_description' );
     90            return array('website_title' => $title);
     91        }
     92        public function cai_upload_from_url($url, $title = null)
     93        {
     94            require_once(ABSPATH . '/wp-admin/includes/image.php');
     95            require_once(ABSPATH . '/wp-admin/includes/file.php');
     96            require_once(ABSPATH . '/wp-admin/includes/media.php');
     97       
     98            // Fetch the remote file
     99            $response = wp_remote_get($url);
     100       
     101            // Check for errors
     102            if (is_wp_error($response)) {
     103                return false;
     104            }
     105       
     106            // Get the filename and extension ("photo.png" => "photo", "png")
     107            $filename = pathinfo($url, PATHINFO_FILENAME);
     108            $extension = pathinfo($url, PATHINFO_EXTENSION);
     109       
     110            // Get the file contents
     111            $file_contents = wp_remote_retrieve_body($response);
     112       
     113            // Upload the file to the WordPress media library
     114            $upload = wp_upload_bits($filename . '.' . $extension, null, $file_contents);
     115       
     116            // Check for errors
     117            if ($upload['error']) {
     118                return false;
     119            }
     120       
     121            // Prepare the attachment
     122            $filetype = wp_check_filetype(basename($upload['file']), null);
     123            $attachment = array(
     124                'guid' => $upload['url'],
     125                'post_mime_type' => $filetype['type'],
     126                'post_title' => preg_replace('/\.[^.]+$/', '', basename($upload['file'])),
     127                'post_content' => '',
     128                'post_status' => 'inherit',
     129            );
     130       
     131            // Insert the attachment
     132            $attachment_id = wp_insert_attachment($attachment, $upload['file']);
     133       
     134            // Generate the metadata for the attachment
     135            $attach_data = wp_generate_attachment_metadata($attachment_id, $upload['file']);
     136       
     137            // Update the attachment metadata
     138            wp_update_attachment_metadata($attachment_id, $attach_data);
     139       
     140            return (int) $attachment_id;
     141        }       
     142
     143
     144        function create_post_with_api_key($data)
     145        {
     146            $author = $data->get_param('author');
     147            $api_key = $data->get_param('api_key');
     148            $title = $data->get_param('title');
     149            $status = $data->get_param('status');
     150            $content = $data->get_param('content');
     151            $keyword = $data->get_param('keyword');
     152            $seo_title = $data->get_param('seo_title');
     153            $seo_description = $data->get_param('seo_description');
     154            $category = $data->get_param('category');
     155            $image = $data->get_param('image');
    90156
    91157            // Validate the API key
    92             if ( ! $this->validate_api_key( $api_key ) ) {
    93                 return new WP_Error( 'invalid_api_key', __( 'The API key provided is invalid', 'contentify-ai' ), array( 'status' => 401 ) );
     158            if (!$this->validate_api_key($api_key)) {
     159                return new WP_Error('invalid_api_key', __('The API key provided is invalid', 'contentify-ai'), array('status' => 401));
    94160            }
    95161
    96162            // Get the user by login or email
    97             $user = get_user_by( 'login', $author );
    98             if ( ! $user ) {
    99                 $user = get_user_by( 'email', $author );
     163            $user = get_user_by('login', $author);
     164            if (!$user) {
     165                $user = get_user_by('email', $author);
    100166            }
    101167            $author_id = $user->ID;
    102168
     169            // Validate post status
     170            $status = strtolower($status);
     171            if (!isset(get_post_statuses()[$status]))
     172                $status = 'publish';
     173
     174            // Category
     175            $term_id = 0;
     176
     177            if (!empty($category)) {
     178                $term = get_term_by('name', $category, 'category');
     179                if (!$term instanceof WP_Term) {
     180                    $term = wp_insert_term($category, 'category');
     181                    if (is_array($term))
     182                        $term_id = $term['term_id'];
     183                } else {
     184                    $term_id = $term->term_id;
     185                }
     186            }
    103187
    104188            // Create the post
    105             $post_id = wp_insert_post( array(
    106                 'post_title' => $title,
    107                 'post_content' => $content,
    108                 'post_status' => $status,
    109                 'post_author' => $author_id,
    110             ) );
     189            $post_id = wp_insert_post(
     190                array(
     191                    'post_title' => $title,
     192                    'post_content' => $content,
     193                    'post_status' => $status,
     194                    'post_author' => $author_id,
     195                    'post_category' => array($term_id)
     196                )
     197            );
    111198
    112199
    113200            //Update Yoast SEO keyword, title, and description
    114             update_post_meta( $post_id, '_yoast_wpseo_focuskw', $keyword );
    115             update_post_meta( $post_id, '_yoast_wpseo_title', $seo_title );
    116             update_post_meta( $post_id, '_yoast_wpseo_metadesc', $seo_description );
     201            update_post_meta($post_id, '_yoast_wpseo_focuskw', $keyword);
     202            update_post_meta($post_id, '_yoast_wpseo_title', $seo_title);
     203            update_post_meta($post_id, '_yoast_wpseo_metadesc', $seo_description);
     204
     205            if (!empty($image)) {
     206                $attachment_id = $this->cai_upload_from_url($image, $title);
     207                if ($attachment_id) {
     208                    set_post_thumbnail($post_id, $attachment_id);
     209                }
     210            }           
    117211
    118212            // Return the post ID
    119             return array( 'post_id' => $post_id );
    120         }
    121 
    122         function contentify_settings_page() {
     213            return array('post_id' => $post_id);
     214        }
     215
     216
     217        function contentify_settings_page()
     218        {
    123219            // Check if the user has the necessary permissions
    124             if ( ! current_user_can( 'manage_options' ) ) {
     220            if (!current_user_can('manage_options')) {
    125221                return;
    126222            }
    127223            ?>
    128224            <div class="wrap">
    129                 <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
     225                <h1>
     226                    <?php echo esc_html(get_admin_page_title()); ?>
     227                </h1>
    130228                <form method="post" action="options.php">
    131229                    <?php
    132230                    // Output nonce, action, and option_page fields for a settings page
    133                     settings_fields( 'contentify_settings' );
     231                    settings_fields('contentify_settings');
    134232                    // Output settings sections and their fields
    135                     do_settings_sections( 'contentify_settings' );
     233                    do_settings_sections('contentify_settings');
    136234                    submit_button();
    137235                    ?>
     
    141239        }
    142240
    143         public function contentify_settings() {
     241        public function contentify_settings()
     242        {
    144243            add_options_page(
    145                 __( 'Contentify Settings', 'contentify-ai' ),
    146                 __( 'Contentify AI', 'contentify-ai' ),
     244                __('Contentify Settings', 'contentify-ai'),
     245                __('Contentify AI', 'contentify-ai'),
    147246                'manage_options',
    148247                'contentify_settings',
    149                 array( $this, 'contentify_settings_page' )
    150             );
    151         }
    152 
    153         public function contentify_settings_section_callback() {
    154             echo __( 'Please enter your Contentify API key to allow the AI to post and optimize your blogs', 'contentify-ai' );
    155         }
    156 
    157 
    158         public function register_contentify_settings() {
    159             register_setting( 'contentify_settings', 'contentify_api_key' );
     248                array($this, 'contentify_settings_page')
     249            );
     250        }
     251
     252        public function contentify_settings_section_callback()
     253        {
     254            echo __('Please enter your Contentify API key to allow the AI to post and optimize your blogs', 'contentify-ai');
     255        }
     256
     257
     258        public function register_contentify_settings()
     259        {
     260            register_setting('contentify_settings', 'contentify_api_key');
    160261            add_settings_section(
    161262                'contentify_settings_section',
    162                 __( 'Contentify API Key', 'contentify-ai' ),
    163                 array( $this, 'contentify_settings_section_callback' ),
     263                __('Contentify API Key', 'contentify-ai'),
     264                array($this, 'contentify_settings_section_callback'),
    164265                'contentify_settings'
    165266            );
    166267            add_settings_field(
    167268                'contentify_api_key',
    168                 __( 'API Key', 'contentify-ai' ),
    169                 array( $this, 'contentify_api_key_callback' ),
     269                __('API Key', 'contentify-ai'),
     270                array($this, 'contentify_api_key_callback'),
    170271                'contentify_settings',
    171272                'contentify_settings_section'
     
    174275
    175276        // Display the API key form field
    176         public function contentify_api_key_callback() {
    177             $api_key = get_option( 'contentify_api_key' );
    178             echo "<input type='password' name='contentify_api_key' value='" . esc_attr( $api_key ) . "'/>";
    179         }
    180 
    181 
    182         public function validate_api_key( $api_key ) {
     277        public function contentify_api_key_callback()
     278        {
     279            $api_key = get_option('contentify_api_key');
     280            echo "<input type='password' name='contentify_api_key' value='" . esc_attr($api_key) . "'/>";
     281        }
     282
     283
     284        public function validate_api_key($api_key)
     285        {
    183286            // Get the user-defined API key
    184             $valid_api_key = get_option( 'contentify_api_key' );
    185             if( ! $api_key ) {
     287            $valid_api_key = get_option('contentify_api_key');
     288            if (!$api_key) {
    186289                return false;
    187290            }
    188291            return $api_key === $valid_api_key;
    189292        }
     293
    190294    }
    191295
  • contentify-ai/trunk/readme.txt

    r2881532 r2885241  
    11=== Contentify AI ===
    22Contributors: alphatrait
    3 Tags: ai, chatgpt, contentify, ai writer
     3Tags: ai, chatgpt, contentify, ai writer, gpt-4, gpt
    44Requires at least: 5.9
    55Tested up to: 6.1.1
Note: See TracChangeset for help on using the changeset viewer.