Changeset 2885241
- Timestamp:
- 03/22/2023 09:40:29 PM (3 years ago)
- Location:
- contentify-ai
- Files:
-
- 2 added
- 2 edited
-
assets/banner-1544x500.png (added)
-
assets/icon-256x256.png (added)
-
trunk/index.php (modified) (4 diffs)
-
trunk/readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
contentify-ai/trunk/index.php
r2881532 r2885241 2 2 /* 3 3 Plugin Name: Contentify AI 4 Description: Allows Co-Writer to write blogs and optimize the posts.5 Version: 1. 0.04 Description: This plugin is a Contentify Editor AI (Co-Editor). It publishes and optimize the content that Contentify Writer AI (Co-Writer) generates. 5 Version: 1.1.4 6 6 Author: Contentify Team 7 7 Text Domain: contentify-ai 8 8 */ 9 9 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 ) { 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 { 18 public static function get_instance() 19 { 20 if (self::$instance == null) { 19 21 self::$instance = new self(); 20 22 } … … 24 26 private static $instance = null; 25 27 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 { 31 38 // Init 32 add_action( 'init', array( $this, 'init' ));39 add_action('init', array($this, 'init')); 33 40 34 41 // 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')); 37 44 38 45 // Register the plugin's settings page 39 add_action( 'admin_menu', array( $this, 'contentify_settings' ));46 add_action('admin_menu', array($this, 'contentify_settings')); 40 47 41 48 // 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( 51 60 'methods' => 'POST', 52 'callback' => array( $this, 'create_post_with_api_key'),61 'callback' => array($this, 'create_post_with_api_key'), 53 62 '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( 59 70 'methods' => 'POST', 60 'callback' => array( $this, 'get_website_title_with_api_key'),71 'callback' => array($this, 'get_website_title_with_api_key'), 61 72 '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'); 67 80 68 81 // 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 73 86 // Use the WordPress function to get the current website title 74 $title = get_option( 'blogname');75 87 $title = get_option('blogname'); 88 76 89 // 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'); 90 156 91 157 // 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)); 94 160 } 95 161 96 162 // 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); 100 166 } 101 167 $author_id = $user->ID; 102 168 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 } 103 187 104 188 // 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 ); 111 198 112 199 113 200 //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 } 117 211 118 212 // 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 { 123 219 // Check if the user has the necessary permissions 124 if ( ! current_user_can( 'manage_options' )) {220 if (!current_user_can('manage_options')) { 125 221 return; 126 222 } 127 223 ?> 128 224 <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> 130 228 <form method="post" action="options.php"> 131 229 <?php 132 230 // Output nonce, action, and option_page fields for a settings page 133 settings_fields( 'contentify_settings');231 settings_fields('contentify_settings'); 134 232 // Output settings sections and their fields 135 do_settings_sections( 'contentify_settings');233 do_settings_sections('contentify_settings'); 136 234 submit_button(); 137 235 ?> … … 141 239 } 142 240 143 public function contentify_settings() { 241 public function contentify_settings() 242 { 144 243 add_options_page( 145 __( 'Contentify Settings', 'contentify-ai'),146 __( 'Contentify AI', 'contentify-ai'),244 __('Contentify Settings', 'contentify-ai'), 245 __('Contentify AI', 'contentify-ai'), 147 246 'manage_options', 148 247 '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'); 160 261 add_settings_section( 161 262 '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'), 164 265 'contentify_settings' 165 266 ); 166 267 add_settings_field( 167 268 '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'), 170 271 'contentify_settings', 171 272 'contentify_settings_section' … … 174 275 175 276 // 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 { 183 286 // 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) { 186 289 return false; 187 290 } 188 291 return $api_key === $valid_api_key; 189 292 } 293 190 294 } 191 295 -
contentify-ai/trunk/readme.txt
r2881532 r2885241 1 1 === Contentify AI === 2 2 Contributors: alphatrait 3 Tags: ai, chatgpt, contentify, ai writer 3 Tags: ai, chatgpt, contentify, ai writer, gpt-4, gpt 4 4 Requires at least: 5.9 5 5 Tested up to: 6.1.1
Note: See TracChangeset
for help on using the changeset viewer.