| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Plugin Name: Writesonic |
| 5 |
* Description: Writesonic WordPress plugin |
| 6 |
* Version: 1.0.6 |
| 7 |
* Author: <a href="https://writesonic.com/">Writesonic</a> |
| 8 |
* Author URI: https://writesonic.com/ |
| 9 |
* Text Domain: writesonic |
| 10 |
* Requires at least: 6.0 |
| 11 |
* Requires PHP: 7.4 |
| 12 |
* License: GPLv2 or later |
| 13 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 14 |
*/ |
| 15 |
|
| 16 |
if (!defined('ABSPATH')) { |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
} |
| 19 |
|
| 20 |
const WRITESONIC_API_KEY_OPTION = 'writesonic_api_key'; |
| 21 |
|
| 22 |
const WRITESONIC_CONNECT_URL = 'https://app.writesonic.com/wordpress-authentication/'; |
| 23 |
// const WRITESONIC_CONNECT_URL = 'http://localhost:3000/wordpress-authentication/'; |
| 24 |
// const WRITESONIC_CONNECT_URL = 'https://staging.writesonic.com/wordpress-authentication/'; |
| 25 |
|
| 26 |
const WRITESONIC_CHECK_AUTH_URL = 'https://api.writesonic.com/v1/thirdparty/wordpress-org-authorization-status'; |
| 27 |
// const WRITESONIC_CHECK_AUTH_URL = 'http://localhost:8081/v1/thirdparty/wordpress-org-authorization-status'; |
| 28 |
// const WRITESONIC_CHECK_AUTH_URL = 'https://dev-backend.writesonic.com/v1/thirdparty/wordpress-org-authorization-status'; |
| 29 |
|
| 30 |
if (!class_exists('WPM_Writesonic_Integration')) { |
| 31 |
class WPM_Writesonic_Integration |
| 32 |
{ |
| 33 |
/** |
| 34 |
* Plugin init, filters, hooks |
| 35 |
*/ |
| 36 |
public static function init() |
| 37 |
{ |
| 38 |
// Initialize option with empty string |
| 39 |
add_option(WRITESONIC_API_KEY_OPTION, ''); |
| 40 |
|
| 41 |
add_action('admin_menu', array('WPM_Writesonic_Integration', 'create_settings_menu')); |
| 42 |
add_action('rest_api_init', array('WPM_Writesonic_Integration', 'register_api_endpoints')); |
| 43 |
register_deactivation_hook(__FILE__, array('WPM_Writesonic_Integration', 'deactivation')); |
| 44 |
add_action('admin_init', array('WPM_Writesonic_Integration', 'register_settings')); |
| 45 |
|
| 46 |
// Get users w/o posts published |
| 47 |
add_filter('rest_user_query', array('WPM_Writesonic_Integration', 'remove_has_published_posts_from_wp_api_user_query'), 10, 2); |
| 48 |
|
| 49 |
// Force custom posts to be visible in REST API |
| 50 |
add_filter('register_post_type_args', array('WPM_Writesonic_Integration', 'custom_post_types_show_in_rest_filter'), 10, 2); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Deactivation hook |
| 55 |
*/ |
| 56 |
public static function deactivation() |
| 57 |
{ |
| 58 |
// We can delete key on deactivation, but it's not needed now |
| 59 |
// delete_option(WRITESONIC_API_KEY_OPTION); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* WP settings registration |
| 64 |
*/ |
| 65 |
public static function register_settings() |
| 66 |
{ |
| 67 |
register_setting('writesonic', WRITESONIC_API_KEY_OPTION); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Settings menu registration |
| 72 |
*/ |
| 73 |
public static function create_settings_menu() |
| 74 |
{ |
| 75 |
add_options_page('Writesonic Settings', 'Writesonic', 'manage_options', 'writesonic', array('WPM_Writesonic_Integration', 'create_settings_page')); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Settings page registration |
| 80 |
*/ |
| 81 |
public static function create_settings_page() |
| 82 |
{ |
| 83 |
include plugin_dir_path(__FILE__) . '/templates/settings.php'; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @param $args |
| 88 |
* @param $post_type |
| 89 |
* |
| 90 |
* @return mixed |
| 91 |
*/ |
| 92 |
public static function custom_post_types_show_in_rest_filter($args, $post_type) |
| 93 |
{ |
| 94 |
$args['show_in_rest'] = true; |
| 95 |
|
| 96 |
return $args; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Removes `has_published_posts` from the query args so even users who have not |
| 101 |
* published content are returned by the request. |
| 102 |
* |
| 103 |
* @see https://developer.wordpress.org/reference/classes/wp_user_query/ |
| 104 |
* |
| 105 |
* @param array $prepared_args Array of arguments for WP_User_Query. |
| 106 |
* @param WP_REST_Request $request The current request. |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
function remove_has_published_posts_from_wp_api_user_query($prepared_args, $request) |
| 111 |
{ |
| 112 |
unset($prepared_args['has_published_posts']); |
| 113 |
|
| 114 |
return $prepared_args; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Register VidApp custom REST API endpoints |
| 119 |
*/ |
| 120 |
public static function register_api_endpoints() |
| 121 |
{ |
| 122 |
/** |
| 123 |
* Categories |
| 124 |
*/ |
| 125 |
$categories_controller = new WP_REST_Terms_Controller('category'); |
| 126 |
register_rest_route('writesonic/v2', '/categories', array( |
| 127 |
'methods' => WP_REST_Server::READABLE, |
| 128 |
'callback' => array('WPM_Writesonic_Integration', 'get_categories'), |
| 129 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_categories_permissions_check'), |
| 130 |
'args' => $categories_controller->get_collection_params() |
| 131 |
)); |
| 132 |
|
| 133 |
$tags_controller = new WP_REST_Terms_Controller('post_tag'); |
| 134 |
register_rest_route('writesonic/v2', '/tags', array( |
| 135 |
'methods' => WP_REST_Server::READABLE, |
| 136 |
'callback' => array('WPM_Writesonic_Integration', 'get_tags'), |
| 137 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_tags_permissions_check'), |
| 138 |
'args' => $tags_controller->get_collection_params() |
| 139 |
)); |
| 140 |
|
| 141 |
/** |
| 142 |
* Posts |
| 143 |
*/ |
| 144 |
$posts_controller = new WP_REST_Posts_Controller('post'); |
| 145 |
register_rest_route('writesonic/v2', '/posts', array( |
| 146 |
array( |
| 147 |
'methods' => WP_REST_Server::READABLE, |
| 148 |
'callback' => array('WPM_Writesonic_Integration', 'get_posts'), |
| 149 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_posts_permission_check'), |
| 150 |
'args' => $posts_controller->get_collection_params() |
| 151 |
), |
| 152 |
array( |
| 153 |
'methods' => WP_REST_Server::CREATABLE, |
| 154 |
'callback' => array('WPM_Writesonic_Integration', 'create_post'), |
| 155 |
'permission_callback' => array('WPM_Writesonic_Integration', 'create_post_permissions_check'), |
| 156 |
'args' => array( |
| 157 |
'title' => array( |
| 158 |
'required' => false, |
| 159 |
'sanitize_callback' => 'sanitize_text_field', |
| 160 |
), |
| 161 |
'content' => array( |
| 162 |
'required' => false, |
| 163 |
), |
| 164 |
'status' => array( |
| 165 |
'required' => false, |
| 166 |
'sanitize_callback' => 'sanitize_text_field', |
| 167 |
), |
| 168 |
'slug' => array( |
| 169 |
'required' => false, |
| 170 |
'sanitize_callback' => 'sanitize_text_field', |
| 171 |
), |
| 172 |
'categories' => array( |
| 173 |
'required' => false, |
| 174 |
'type' => 'array', |
| 175 |
), |
| 176 |
'tags' => array( |
| 177 |
'required' => false, |
| 178 |
'type' => 'array', |
| 179 |
), |
| 180 |
'meta_description' => array( |
| 181 |
'required' => false, |
| 182 |
'sanitize_callback' => 'sanitize_text_field', |
| 183 |
), |
| 184 |
'featured_image' => array( |
| 185 |
'required' => false, |
| 186 |
'type' => 'string', |
| 187 |
), |
| 188 |
), |
| 189 |
), |
| 190 |
'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'), |
| 191 |
)); |
| 192 |
|
| 193 |
/** |
| 194 |
* Posts update |
| 195 |
*/ |
| 196 |
register_rest_route( |
| 197 |
'writesonic/v2', |
| 198 |
'/posts/(?P<id>\d+)', |
| 199 |
array( |
| 200 |
array( |
| 201 |
'methods' => WP_REST_Server::READABLE, |
| 202 |
'callback' => array('WPM_Writesonic_Integration', 'get_post'), |
| 203 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_post_permission_check'), |
| 204 |
), |
| 205 |
array( |
| 206 |
'methods' => WP_REST_Server::EDITABLE, |
| 207 |
'callback' => array('WPM_Writesonic_Integration', 'update_post'), |
| 208 |
'permission_callback' => array('WPM_Writesonic_Integration', 'update_post_permissions_check'), |
| 209 |
'args' => array( |
| 210 |
'title' => array( |
| 211 |
'required' => false, |
| 212 |
'sanitize_callback' => 'sanitize_text_field', |
| 213 |
), |
| 214 |
'content' => array( |
| 215 |
'required' => false, |
| 216 |
), |
| 217 |
'status' => array( |
| 218 |
'required' => false, |
| 219 |
'sanitize_callback' => 'sanitize_text_field', |
| 220 |
), |
| 221 |
'slug' => array( |
| 222 |
'required' => false, |
| 223 |
'sanitize_callback' => 'sanitize_text_field', |
| 224 |
), |
| 225 |
'categories' => array( |
| 226 |
'required' => false, |
| 227 |
'type' => 'array', |
| 228 |
), |
| 229 |
'tags' => array( |
| 230 |
'required' => false, |
| 231 |
'type' => 'array', |
| 232 |
), |
| 233 |
'meta_description' => array( |
| 234 |
'required' => false, |
| 235 |
'sanitize_callback' => 'sanitize_text_field', |
| 236 |
), |
| 237 |
'featured_image' => array( |
| 238 |
'required' => false, |
| 239 |
'type' => 'string', |
| 240 |
), |
| 241 |
), |
| 242 |
'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'), |
| 243 |
), |
| 244 |
) |
| 245 |
); |
| 246 |
|
| 247 |
/** |
| 248 |
* Media |
| 249 |
*/ |
| 250 |
$attachment_controller = new WP_REST_Attachments_Controller('attachment'); |
| 251 |
register_rest_route('writesonic/v2', '/media', array( |
| 252 |
array( |
| 253 |
'methods' => WP_REST_Server::READABLE, |
| 254 |
'callback' => array('WPM_Writesonic_Integration', 'get_media'), |
| 255 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_media_permission_check'), |
| 256 |
'args' => $attachment_controller->get_collection_params() |
| 257 |
|
| 258 |
), |
| 259 |
array( |
| 260 |
'methods' => WP_REST_Server::CREATABLE, |
| 261 |
'callback' => array('WPM_Writesonic_Integration', 'create_media'), |
| 262 |
'permission_callback' => array('WPM_Writesonic_Integration', 'create_media_permissions_check'), |
| 263 |
'args' => $attachment_controller->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE), |
| 264 |
), |
| 265 |
'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'), |
| 266 |
)); |
| 267 |
|
| 268 |
/** |
| 269 |
* Comments |
| 270 |
*/ |
| 271 |
$comment_controller = new WP_REST_Comments_Controller(); |
| 272 |
register_rest_route('writesonic/v2', '/comments', array( |
| 273 |
array( |
| 274 |
'methods' => WP_REST_Server::READABLE, |
| 275 |
'callback' => array('WPM_Writesonic_Integration', 'get_comments'), |
| 276 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_comments_permission_check'), |
| 277 |
'args' => $comment_controller->get_collection_params() |
| 278 |
|
| 279 |
), |
| 280 |
array( |
| 281 |
'methods' => WP_REST_Server::CREATABLE, |
| 282 |
'callback' => array('WPM_Writesonic_Integration', 'create_comment'), |
| 283 |
'permission_callback' => array('WPM_Writesonic_Integration', 'create_comment_permissions_check'), |
| 284 |
'args' => $comment_controller->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE), |
| 285 |
), |
| 286 |
'schema' => array('WPM_Writesonic_Integration', 'get_public_item_schema'), |
| 287 |
)); |
| 288 |
|
| 289 |
/** |
| 290 |
* Users |
| 291 |
*/ |
| 292 |
$users_controller = new WP_REST_Users_Controller(); |
| 293 |
register_rest_route('writesonic/v2', '/users', array( |
| 294 |
'methods' => WP_REST_Server::READABLE, |
| 295 |
'callback' => array('WPM_Writesonic_Integration', 'get_users'), |
| 296 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_users_permissions_check'), |
| 297 |
'args' => $users_controller->get_collection_params() |
| 298 |
)); |
| 299 |
|
| 300 |
register_rest_route('writesonic/v2', '/password', array( |
| 301 |
'methods' => WP_REST_Server::CREATABLE, |
| 302 |
'callback' => array('WPM_Writesonic_Integration', 'validate_user_password'), |
| 303 |
'permission_callback' => array('WPM_Writesonic_Integration', 'validate_password_permissions_check'), |
| 304 |
'args' => array( |
| 305 |
'password' => array( |
| 306 |
'default' => null, // значение параметра по умолчанию |
| 307 |
'required' => true, // является ли параметр обязательным. Может быть только true |
| 308 |
'sanitize_callback' => 'sanitize_text_field', // функция очистки значения параметра. Должна вернуть очищенное значение |
| 309 |
) |
| 310 |
) |
| 311 |
)); |
| 312 |
|
| 313 |
register_rest_route('writesonic/v2', '/authors', array( |
| 314 |
'methods' => WP_REST_Server::READABLE, |
| 315 |
'callback' => array('WPM_Writesonic_Integration', 'get_authors'), |
| 316 |
'permission_callback' => array('WPM_Writesonic_Integration', 'get_authors_permissions_check'), |
| 317 |
'args' => $users_controller->get_collection_params() |
| 318 |
)); |
| 319 |
} |
| 320 |
|
| 321 |
public static function get_user_by_token($token, $user) |
| 322 |
{ |
| 323 |
$wpb_writesonic_tokens = get_option(WRITESONIC_API_KEY_OPTION); |
| 324 |
|
| 325 |
if (!is_array($wpb_writesonic_tokens)) { |
| 326 |
return $user; |
| 327 |
} |
| 328 |
|
| 329 |
$user_email = array_search($token, $wpb_writesonic_tokens); |
| 330 |
|
| 331 |
if ($user_email) { |
| 332 |
$user = get_user_by('email', $user_email); |
| 333 |
|
| 334 |
return $user->ID; |
| 335 |
} |
| 336 |
|
| 337 |
return $user; |
| 338 |
} |
| 339 |
|
| 340 |
public static function get_public_item_schema() |
| 341 |
{ |
| 342 |
$posts_controller = new WP_REST_Posts_Controller('post'); |
| 343 |
|
| 344 |
return $posts_controller->get_public_item_schema(); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* @param WP_REST_Request $request |
| 349 |
* |
| 350 |
* @return bool |
| 351 |
*/ |
| 352 |
public static function get_users_permissions_check(WP_REST_Request $request) |
| 353 |
{ |
| 354 |
return self::checkAPIKeyAuth($request); |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* @param WP_REST_Request $request |
| 359 |
* |
| 360 |
* @return bool |
| 361 |
*/ |
| 362 |
public static function get_categories_permissions_check(WP_REST_Request $request) |
| 363 |
{ |
| 364 |
return self::checkAPIKeyAuth($request); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* @param WP_REST_Request $request |
| 369 |
* |
| 370 |
* @return mixed |
| 371 |
*/ |
| 372 |
public static function get_users(WP_REST_Request $request) |
| 373 |
{ |
| 374 |
$controller = new WP_REST_Users_Controller(); |
| 375 |
|
| 376 |
$response = $controller->get_items($request); |
| 377 |
|
| 378 |
return $response; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Get users who can create or edit posts |
| 383 |
* |
| 384 |
* @param WP_REST_Request $request |
| 385 |
* @return WP_REST_Response |
| 386 |
*/ |
| 387 |
public static function get_authors(WP_REST_Request $request) |
| 388 |
{ |
| 389 |
// Define the query arguments to get users with post creation/editing capabilities |
| 390 |
$args = array( |
| 391 |
'role__in' => array('administrator', 'editor', 'author'), // Roles capable of creating/editing posts |
| 392 |
); |
| 393 |
|
| 394 |
// Fetch the users |
| 395 |
$users = get_users($args); |
| 396 |
|
| 397 |
// Prepare the response data |
| 398 |
$author_data = array_map(function ($user) { |
| 399 |
return array( |
| 400 |
'ID' => $user->ID, |
| 401 |
'username' => $user->user_login, |
| 402 |
'name' => $user->display_name, |
| 403 |
'email' => $user->user_email, |
| 404 |
); |
| 405 |
}, $users); |
| 406 |
|
| 407 |
// Return the response |
| 408 |
return rest_ensure_response($author_data); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Permissions check for getting authors |
| 413 |
* |
| 414 |
* @param WP_REST_Request $request |
| 415 |
* @return bool |
| 416 |
*/ |
| 417 |
public static function get_authors_permissions_check(WP_REST_Request $request) |
| 418 |
{ |
| 419 |
return self::checkAPIKeyAuth($request); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
/** |
| 424 |
* Get all categories |
| 425 |
* |
| 426 |
* @param WP_REST_Request $request |
| 427 |
* @return WP_REST_Response |
| 428 |
*/ |
| 429 |
public static function get_categories(WP_REST_Request $request) |
| 430 |
{ |
| 431 |
// Define arguments to fetch all categories |
| 432 |
$args = array( |
| 433 |
'hide_empty' => false, // Include categories even if they have no posts |
| 434 |
); |
| 435 |
|
| 436 |
// Fetch all categories |
| 437 |
$categories = get_categories($args); |
| 438 |
|
| 439 |
// Prepare the response data |
| 440 |
$category_data = array_map(function ($category) { |
| 441 |
return array( |
| 442 |
'ID' => $category->term_id, |
| 443 |
'name' => $category->name, |
| 444 |
'slug' => $category->slug, |
| 445 |
'count' => $category->count, // Number of posts in the category |
| 446 |
); |
| 447 |
}, $categories); |
| 448 |
|
| 449 |
// Return the response |
| 450 |
return rest_ensure_response($category_data); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Get all tags |
| 455 |
* |
| 456 |
* @param WP_REST_Request $request |
| 457 |
* @return WP_REST_Response |
| 458 |
*/ |
| 459 |
public static function get_tags(WP_REST_Request $request) |
| 460 |
{ |
| 461 |
// Define arguments to fetch all tags |
| 462 |
$args = array( |
| 463 |
'hide_empty' => false, // Include tags even if they have no posts |
| 464 |
); |
| 465 |
|
| 466 |
// Fetch all tags |
| 467 |
$tags = get_tags($args); |
| 468 |
|
| 469 |
// Prepare the response data |
| 470 |
$tag_data = array_map(function ($tag) { |
| 471 |
return array( |
| 472 |
'ID' => $tag->term_id, |
| 473 |
'name' => $tag->name, |
| 474 |
'slug' => $tag->slug, |
| 475 |
); |
| 476 |
}, $tags); |
| 477 |
|
| 478 |
// Return the response |
| 479 |
return rest_ensure_response($tag_data); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Permissions check for getting tags |
| 484 |
* |
| 485 |
* @param WP_REST_Request $request |
| 486 |
* @return bool |
| 487 |
*/ |
| 488 |
public static function get_tags_permissions_check(WP_REST_Request $request) |
| 489 |
{ |
| 490 |
return self::checkAPIKeyAuth($request); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
/** |
| 496 |
* @param WP_REST_Request $request |
| 497 |
* |
| 498 |
* @return mixed |
| 499 |
*/ |
| 500 |
public static function get_posts_permission_check(WP_REST_Request $request) |
| 501 |
{ |
| 502 |
return self::checkAPIKeyAuth($request); |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* @param WP_REST_Request $request |
| 507 |
* |
| 508 |
* @return mixed |
| 509 |
*/ |
| 510 |
public static function get_comments(WP_REST_Request $request) |
| 511 |
{ |
| 512 |
$controller = new WP_REST_Comments_Controller(); |
| 513 |
$response = $controller->get_items($request); |
| 514 |
|
| 515 |
return $response; |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* @param WP_REST_Request $request |
| 520 |
* |
| 521 |
* @return bool |
| 522 |
*/ |
| 523 |
public static function get_comments_permission_check(WP_REST_Request $request) |
| 524 |
{ |
| 525 |
return self::checkAPIKeyAuth($request); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Get posts of any status and include tag and category names |
| 530 |
* |
| 531 |
* @param WP_REST_Request $request |
| 532 |
* @return WP_REST_Response |
| 533 |
*/ |
| 534 |
public static function get_posts(WP_REST_Request $request) |
| 535 |
{ |
| 536 |
// Determine the post type from the request, default to 'post' |
| 537 |
$post_type = isset($request['post_type']) ? sanitize_text_field($request['post_type']) : 'post'; |
| 538 |
|
| 539 |
// Set the status parameter to 'any' to fetch posts of any status |
| 540 |
$request['status'] = 'any'; |
| 541 |
|
| 542 |
// Create a new WP_REST_Posts_Controller for the specified post type |
| 543 |
$controller = new WP_REST_Posts_Controller($post_type); |
| 544 |
|
| 545 |
// Fetch the posts using the modified request parameters |
| 546 |
$response = $controller->get_items($request); |
| 547 |
|
| 548 |
// Check if the response is successful |
| 549 |
if ($response instanceof WP_REST_Response && $response->is_error()) { |
| 550 |
return $response; |
| 551 |
} |
| 552 |
|
| 553 |
// Get the posts from the response |
| 554 |
$posts = $response->get_data(); |
| 555 |
|
| 556 |
// Loop through each post to fetch and append category and tag names |
| 557 |
foreach ($posts as &$post) { |
| 558 |
// Fetch category names |
| 559 |
$categories = get_the_category($post['id']); |
| 560 |
$category_names = array_map(function ($cat) { |
| 561 |
return $cat->name; |
| 562 |
}, $categories); |
| 563 |
|
| 564 |
// Fetch tag names |
| 565 |
$tags = get_the_tags($post['id']); |
| 566 |
$tag_names = array(); |
| 567 |
if ($tags) { |
| 568 |
$tag_names = array_map(function ($tag) { |
| 569 |
return $tag->name; |
| 570 |
}, $tags); |
| 571 |
} |
| 572 |
|
| 573 |
// Replace the IDs with names in the post data |
| 574 |
$post['categories'] = $category_names; |
| 575 |
$post['tags'] = $tag_names; |
| 576 |
|
| 577 |
// * get the meta description and add it to the response |
| 578 |
$meta_description = get_post_meta($post['id'], 'description', true); |
| 579 |
// * if it's not empty, add it to the response |
| 580 |
if (!empty($meta_description)) { |
| 581 |
$post['meta_description'] = $meta_description; |
| 582 |
} |
| 583 |
|
| 584 |
if (!empty($post['featured_media'])) { |
| 585 |
// * we need to get the link to the image |
| 586 |
$featured_image = wp_get_attachment_image_src($post['featured_media'], 'full'); |
| 587 |
$post['featured_image'] = $featured_image[0]; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
// Return the modified response |
| 592 |
return rest_ensure_response($posts); |
| 593 |
} |
| 594 |
|
| 595 |
public static function get_post(WP_REST_Request $request) |
| 596 |
{ |
| 597 |
$controller = new WP_REST_Posts_Controller('post'); |
| 598 |
$response = $controller->get_item($request); |
| 599 |
|
| 600 |
return $response; |
| 601 |
} |
| 602 |
|
| 603 |
public static function get_post_permission_check(WP_REST_Request $request) |
| 604 |
{ |
| 605 |
return self::checkAPIKeyAuth($request); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Check if correct API key provided in request |
| 610 |
* |
| 611 |
* @param WP_REST_Request $request |
| 612 |
* |
| 613 |
* @return bool |
| 614 |
*/ |
| 615 |
public static function checkAPIKeyAuth(WP_REST_Request $request) |
| 616 |
{ |
| 617 |
$auth = isset($_SERVER['HTTP_TOKEN']) ? sanitize_text_field($_SERVER['HTTP_TOKEN']) : false; |
| 618 |
$writesonic_token_key = get_option(WRITESONIC_API_KEY_OPTION, true); |
| 619 |
|
| 620 |
wp_set_current_user(self::get_user_by_token($auth, $user)); |
| 621 |
|
| 622 |
if (is_array($writesonic_token_key) && in_array($auth, $writesonic_token_key)) { |
| 623 |
return true; |
| 624 |
} |
| 625 |
|
| 626 |
return false; |
| 627 |
} |
| 628 |
|
| 629 |
/** |
| 630 |
* Handle the creation or updating of a post, including handling mixed input for tags and categories |
| 631 |
* |
| 632 |
* @param WP_REST_Request $request |
| 633 |
* @param bool $is_update Whether this is an update operation |
| 634 |
* @return WP_REST_Response |
| 635 |
*/ |
| 636 |
private static function handle_post_creation_or_update(WP_REST_Request $request, $is_update = false) |
| 637 |
{ |
| 638 |
// Determine the post type from the request, default to 'post' |
| 639 |
$post_type = isset($request['post_type']) ? sanitize_text_field($request['post_type']) : 'post'; |
| 640 |
|
| 641 |
// Prepare the parameters for creating or updating the post |
| 642 |
$params = array( |
| 643 |
'post_type' => $post_type, |
| 644 |
'post_title' => isset($request['title']) ? sanitize_text_field($request['title']) : '', |
| 645 |
'post_content' => isset($request['content']) ? $request['content'] : '', |
| 646 |
'post_status' => isset($request['status']) ? sanitize_text_field($request['status']) : 'draft', |
| 647 |
); |
| 648 |
|
| 649 |
// Handle the optional slug parameter |
| 650 |
if (isset($request['slug'])) { |
| 651 |
$params['post_name'] = sanitize_text_field($request['slug']); |
| 652 |
} |
| 653 |
|
| 654 |
// Process categories (names as strings) |
| 655 |
if (isset($request['categories']) && is_array($request['categories'])) { |
| 656 |
$category_ids = array(); |
| 657 |
foreach ($request['categories'] as $category_name) { |
| 658 |
// Try to get the category by name |
| 659 |
$category = get_term_by('name', sanitize_text_field($category_name), 'category'); |
| 660 |
if ($category) { |
| 661 |
// Existing category, use its ID |
| 662 |
$category_ids[] = $category->term_id; |
| 663 |
} else { |
| 664 |
// New category, create it |
| 665 |
$new_category = wp_insert_term(sanitize_text_field($category_name), 'category'); |
| 666 |
if (!is_wp_error($new_category)) { |
| 667 |
$category_ids[] = $new_category['term_id']; |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
$params['post_category'] = $category_ids; |
| 672 |
} |
| 673 |
|
| 674 |
// Process tags (names as strings) |
| 675 |
if (isset($request['tags']) && is_array($request['tags'])) { |
| 676 |
$tag_ids = array(); |
| 677 |
foreach ($request['tags'] as $tag_name) { |
| 678 |
// Try to get the tag by name |
| 679 |
$tag = get_term_by('name', sanitize_text_field($tag_name), 'post_tag'); |
| 680 |
if ($tag) { |
| 681 |
// Existing tag, use its ID |
| 682 |
$tag_ids[] = $tag->term_id; |
| 683 |
} else { |
| 684 |
// New tag, create it |
| 685 |
$new_tag = wp_insert_term(sanitize_text_field($tag_name), 'post_tag'); |
| 686 |
if (!is_wp_error($new_tag)) { |
| 687 |
$tag_ids[] = $new_tag['term_id']; |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
$params['tax_input'] = array('post_tag' => $tag_ids); |
| 692 |
} |
| 693 |
|
| 694 |
if (isset($request['author']) && is_numeric($request['author'])) { |
| 695 |
$params['post_author'] = (int) $request['author']; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
// If this is an update, include the post ID |
| 700 |
if ($is_update) { |
| 701 |
$post_id = (int) $request['id']; |
| 702 |
if (!get_post($post_id)) { |
| 703 |
return new WP_REST_Response(array( |
| 704 |
'message' => 'Post not found' |
| 705 |
), 404); |
| 706 |
} |
| 707 |
$params['ID'] = $post_id; |
| 708 |
} |
| 709 |
|
| 710 |
// Create or update the post |
| 711 |
$result = $is_update ? wp_update_post($params, true) : wp_insert_post($params, true); |
| 712 |
|
| 713 |
if (is_wp_error($result)) { |
| 714 |
return new WP_REST_Response(array( |
| 715 |
'message' => $result->get_error_message() |
| 716 |
), 400); |
| 717 |
} |
| 718 |
|
| 719 |
// * update the meta description. |
| 720 |
if (isset($request['meta_description'])) { |
| 721 |
update_post_meta($result, 'description', sanitize_text_field($request['meta_description'])); |
| 722 |
} |
| 723 |
if (isset($request['featured_image'])) { |
| 724 |
$attachment_id = (int) $request['featured_image']; |
| 725 |
if ($attachment_id) { |
| 726 |
set_post_thumbnail($result, $attachment_id); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
// Fetch the newly created or updated post to return as a response |
| 731 |
$controller = new WP_REST_Posts_Controller($post_type); |
| 732 |
$request->set_param('id', $result); |
| 733 |
$response = $controller->prepare_item_for_response(get_post($result), $request); |
| 734 |
|
| 735 |
return $response; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
/** |
| 741 |
* @param WP_REST_Request $request |
| 742 |
* |
| 743 |
* @return bool |
| 744 |
*/ |
| 745 |
public static function create_post_permissions_check(WP_REST_Request $request) |
| 746 |
{ |
| 747 |
return self::checkAPIKeyAuth($request); |
| 748 |
} |
| 749 |
|
| 750 |
/** |
| 751 |
* @param WP_REST_Request $request |
| 752 |
* |
| 753 |
* @return mixed |
| 754 |
*/ |
| 755 |
public static function create_post(WP_REST_Request $request) |
| 756 |
{ |
| 757 |
return self::handle_post_creation_or_update($request, false); |
| 758 |
} |
| 759 |
|
| 760 |
/** |
| 761 |
* Update an existing post |
| 762 |
* |
| 763 |
* @param WP_REST_Request $request |
| 764 |
* @return WP_REST_Response |
| 765 |
*/ |
| 766 |
public static function update_post(WP_REST_Request $request) |
| 767 |
{ |
| 768 |
return self::handle_post_creation_or_update($request, true); |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Permissions check for updating a post |
| 773 |
* |
| 774 |
* @param WP_REST_Request $request |
| 775 |
* @return bool |
| 776 |
*/ |
| 777 |
public static function update_post_permissions_check(WP_REST_Request $request) |
| 778 |
{ |
| 779 |
return self::checkAPIKeyAuth($request); |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
/** |
| 784 |
* @param WP_REST_Request $request |
| 785 |
* |
| 786 |
* @return mixed |
| 787 |
*/ |
| 788 |
public static function get_media_permission_check(WP_REST_Request $request) |
| 789 |
{ |
| 790 |
return self::checkAPIKeyAuth($request); |
| 791 |
} |
| 792 |
|
| 793 |
/** |
| 794 |
* @param WP_REST_Request $request |
| 795 |
* |
| 796 |
* @return mixed |
| 797 |
*/ |
| 798 |
public static function get_media(WP_REST_Request $request) |
| 799 |
{ |
| 800 |
$controller = new WP_REST_Attachments_Controller('attachment'); |
| 801 |
$response = $controller->get_items($request); |
| 802 |
|
| 803 |
return $response; |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* @param WP_REST_Request $request |
| 808 |
* |
| 809 |
* @return mixed |
| 810 |
*/ |
| 811 |
public static function create_media_permissions_check(WP_REST_Request $request) |
| 812 |
{ |
| 813 |
return self::checkAPIKeyAuth($request); |
| 814 |
} |
| 815 |
|
| 816 |
/** |
| 817 |
* @param WP_REST_Request $request |
| 818 |
* |
| 819 |
* @return mixed |
| 820 |
*/ |
| 821 |
public static function create_media(WP_REST_Request $request) |
| 822 |
{ |
| 823 |
$controller = new WP_REST_Attachments_Controller('attachment'); |
| 824 |
$response = $controller->create_item($request); |
| 825 |
|
| 826 |
return $response; |
| 827 |
} |
| 828 |
|
| 829 |
/** |
| 830 |
* @param WP_REST_Request $request |
| 831 |
* |
| 832 |
* @return mixed |
| 833 |
*/ |
| 834 |
public static function create_comment_permissions_check(WP_REST_Request $request) |
| 835 |
{ |
| 836 |
return self::checkAPIKeyAuth($request); |
| 837 |
} |
| 838 |
|
| 839 |
/** |
| 840 |
* @param WP_REST_Request $request |
| 841 |
* |
| 842 |
* @return mixed |
| 843 |
*/ |
| 844 |
public static function create_comment(WP_REST_Request $request) |
| 845 |
{ |
| 846 |
$controller = new WP_REST_Comments_Controller(); |
| 847 |
$response = $controller->create_item($request); |
| 848 |
|
| 849 |
return $response; |
| 850 |
} |
| 851 |
|
| 852 |
/** |
| 853 |
* @param WP_REST_Request $request |
| 854 |
* |
| 855 |
* @return bool |
| 856 |
*/ |
| 857 |
public static function validate_password_permissions_check(WP_REST_Request $request) |
| 858 |
{ |
| 859 |
return self::checkAPIKeyAuth($request); |
| 860 |
} |
| 861 |
|
| 862 |
/** |
| 863 |
* Validate user's password |
| 864 |
* |
| 865 |
* @param WP_REST_Request $request |
| 866 |
* @return string |
| 867 |
*/ |
| 868 |
public static function validate_user_password(WP_REST_Request $request) |
| 869 |
{ |
| 870 |
if (!isset($request['password']) || !isset($request['user_id'])) { |
| 871 |
return []; |
| 872 |
} |
| 873 |
|
| 874 |
$password = $request['password']; |
| 875 |
$user = get_user_by('id', $request['user_id']); |
| 876 |
if (wp_check_password($password, $user->user_pass)) { |
| 877 |
$data = array('result' => 'true'); |
| 878 |
} else { |
| 879 |
$data = array('result' => 'false'); |
| 880 |
} |
| 881 |
|
| 882 |
$response = rest_ensure_response($data); |
| 883 |
|
| 884 |
wp_send_json($response); |
| 885 |
} |
| 886 |
|
| 887 |
/** |
| 888 |
* Check authorization |
| 889 |
* |
| 890 |
* @param string $token |
| 891 |
* @param string $domain |
| 892 |
* @return bool |
| 893 |
*/ |
| 894 |
public static function checkAuthorization($token, $domain) |
| 895 |
{ |
| 896 |
$body = [ |
| 897 |
'token' => $token, |
| 898 |
'domain' => $domain, |
| 899 |
]; |
| 900 |
|
| 901 |
$body = wp_json_encode($body); |
| 902 |
|
| 903 |
$options = [ |
| 904 |
'body' => $body, |
| 905 |
'headers' => [ |
| 906 |
'Content-Type' => 'application/json', |
| 907 |
], |
| 908 |
'data_format' => 'body', |
| 909 |
]; |
| 910 |
|
| 911 |
$request = wp_remote_post(WRITESONIC_CHECK_AUTH_URL, $options); |
| 912 |
|
| 913 |
$tmp = json_decode(wp_remote_retrieve_body($request), true); |
| 914 |
|
| 915 |
return (bool) $tmp; |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
WPM_Writesonic_Integration::init(); |
| 920 |
} |
| 921 |
|