writesonic.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Plugin Name: Writesonic |
| 5 | * Description: Writesonic WordPress plugin |
| 6 | * Version: 1.0.1 |
| 7 | * Author: <a href="https://writesonic.com/">Writesonic</a> |
| 8 | * Author URI: https://writesonic.com/ |
| 9 | * Text Domain: writesonic |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly. |
| 14 | } |
| 15 | |
| 16 | const WRITESONIC_API_KEY_OPTION = 'writesonic_api_key'; |
| 17 | const WRITESONIC_CONNECT_URL = 'https://app.writesonic.com/wordpress-authentication/'; |
| 18 | const WRITESONIC_CHECK_AUTH_URL = 'http://api.writesonic.com/v1/thirdparty/wordpress-org-authorization-status'; |
| 19 | |
| 20 | if ( ! class_exists( 'WPM_Writesonic_Integration' ) ) { |
| 21 | class WPM_Writesonic_Integration { |
| 22 | /** |
| 23 | * Plugin init, filters, hooks |
| 24 | */ |
| 25 | public static function init() { |
| 26 | // Initialize option with empty string |
| 27 | add_option( WRITESONIC_API_KEY_OPTION, '' ); |
| 28 | |
| 29 | add_action( 'admin_menu', array( 'WPM_Writesonic_Integration', 'create_settings_menu' ) ); |
| 30 | add_action( 'rest_api_init', array( 'WPM_Writesonic_Integration', 'register_api_endpoints' ) ); |
| 31 | register_deactivation_hook( __FILE__, array( 'WPM_Writesonic_Integration', 'deactivation' ) ); |
| 32 | add_action( 'admin_init', array( 'WPM_Writesonic_Integration', 'register_settings' ) ); |
| 33 | |
| 34 | // Get users w/o posts published |
| 35 | add_filter( 'rest_user_query', array( 'WPM_Writesonic_Integration', 'remove_has_published_posts_from_wp_api_user_query' ), 10, 2 ); |
| 36 | |
| 37 | // Force custom posts to be visible in REST API |
| 38 | add_filter( 'register_post_type_args', array( 'WPM_Writesonic_Integration', 'custom_post_types_show_in_rest_filter' ), 10, 2 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Deactivation hook |
| 43 | */ |
| 44 | public static function deactivation() { |
| 45 | // We can delete key on deactivation, but it's not needed now |
| 46 | // delete_option(WRITESONIC_API_KEY_OPTION); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * WP settings registration |
| 51 | */ |
| 52 | public static function register_settings() { |
| 53 | register_setting( 'writesonic', WRITESONIC_API_KEY_OPTION ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Settings menu registration |
| 58 | */ |
| 59 | public static function create_settings_menu() { |
| 60 | add_options_page( 'Writesonic Settings', 'Writesonic', 'manage_options', 'writesonic', array( 'WPM_Writesonic_Integration', 'create_settings_page' ) ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Settings page registration |
| 65 | */ |
| 66 | public static function create_settings_page() { |
| 67 | include plugin_dir_path( __FILE__ ) . '/templates/settings.php'; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param $args |
| 72 | * @param $post_type |
| 73 | * |
| 74 | * @return mixed |
| 75 | */ |
| 76 | public static function custom_post_types_show_in_rest_filter( $args, $post_type ) { |
| 77 | $args['show_in_rest'] = true; |
| 78 | |
| 79 | return $args; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Removes `has_published_posts` from the query args so even users who have not |
| 84 | * published content are returned by the request. |
| 85 | * |
| 86 | * @see https://developer.wordpress.org/reference/classes/wp_user_query/ |
| 87 | * |
| 88 | * @param array $prepared_args Array of arguments for WP_User_Query. |
| 89 | * @param WP_REST_Request $request The current request. |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | function remove_has_published_posts_from_wp_api_user_query( $prepared_args, $request ) { |
| 94 | unset( $prepared_args['has_published_posts'] ); |
| 95 | |
| 96 | return $prepared_args; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Register VidApp custom REST API endpoints |
| 101 | */ |
| 102 | public static function register_api_endpoints() { |
| 103 | /** |
| 104 | * Categories |
| 105 | */ |
| 106 | $categories_controller = new WP_REST_Terms_Controller( 'category' ); |
| 107 | register_rest_route( 'writesonic/v2', '/categories', array( |
| 108 | 'methods' => WP_REST_Server::READABLE, |
| 109 | 'callback' => array( 'WPM_Writesonic_Integration', 'get_categories' ), |
| 110 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'get_categories_permissions_check' ), |
| 111 | 'args' => $categories_controller->get_collection_params() |
| 112 | ) ); |
| 113 | |
| 114 | /** |
| 115 | * Posts |
| 116 | */ |
| 117 | $posts_controller = new WP_REST_Posts_Controller( 'post' ); |
| 118 | register_rest_route( 'writesonic/v2', '/posts', array( |
| 119 | array( |
| 120 | 'methods' => WP_REST_Server::READABLE, |
| 121 | 'callback' => array( 'WPM_Writesonic_Integration', 'get_posts' ), |
| 122 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'get_posts_permission_check' ), |
| 123 | 'args' => $posts_controller->get_collection_params() |
| 124 | |
| 125 | ), |
| 126 | array( |
| 127 | 'methods' => WP_REST_Server::CREATABLE, |
| 128 | 'callback' => array( 'WPM_Writesonic_Integration', 'create_post' ), |
| 129 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'create_post_permissions_check' ), |
| 130 | 'args' => $posts_controller->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 131 | ), |
| 132 | 'schema' => array( 'WPM_Writesonic_Integration', 'get_public_item_schema' ), |
| 133 | ) ); |
| 134 | |
| 135 | /** |
| 136 | * Media |
| 137 | */ |
| 138 | $attachment_controller = new WP_REST_Attachments_Controller( 'attachment' ); |
| 139 | register_rest_route( 'writesonic/v2', '/media', array( |
| 140 | array( |
| 141 | 'methods' => WP_REST_Server::READABLE, |
| 142 | 'callback' => array( 'WPM_Writesonic_Integration', 'get_media' ), |
| 143 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'get_media_permission_check' ), |
| 144 | 'args' => $attachment_controller->get_collection_params() |
| 145 | |
| 146 | ), |
| 147 | array( |
| 148 | 'methods' => WP_REST_Server::CREATABLE, |
| 149 | 'callback' => array( 'WPM_Writesonic_Integration', 'create_media' ), |
| 150 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'create_media_permissions_check' ), |
| 151 | 'args' => $attachment_controller->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 152 | ), |
| 153 | 'schema' => array( 'WPM_Writesonic_Integration', 'get_public_item_schema' ), |
| 154 | ) ); |
| 155 | |
| 156 | /** |
| 157 | * Comments |
| 158 | */ |
| 159 | $comment_controller = new WP_REST_Comments_Controller(); |
| 160 | register_rest_route( 'writesonic/v2', '/comments', array( |
| 161 | array( |
| 162 | 'methods' => WP_REST_Server::READABLE, |
| 163 | 'callback' => array( 'WPM_Writesonic_Integration', 'get_comments' ), |
| 164 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'get_comments_permission_check' ), |
| 165 | 'args' => $comment_controller->get_collection_params() |
| 166 | |
| 167 | ), |
| 168 | array( |
| 169 | 'methods' => WP_REST_Server::CREATABLE, |
| 170 | 'callback' => array( 'WPM_Writesonic_Integration', 'create_comment' ), |
| 171 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'create_comment_permissions_check' ), |
| 172 | 'args' => $comment_controller->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 173 | ), |
| 174 | 'schema' => array( 'WPM_Writesonic_Integration', 'get_public_item_schema' ), |
| 175 | ) ); |
| 176 | |
| 177 | /** |
| 178 | * Users |
| 179 | */ |
| 180 | $users_controller = new WP_REST_Users_Controller(); |
| 181 | register_rest_route( 'writesonic/v2', '/users', array( |
| 182 | 'methods' => WP_REST_Server::READABLE, |
| 183 | 'callback' => array( 'WPM_Writesonic_Integration', 'get_users' ), |
| 184 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'get_users_permissions_check' ), |
| 185 | 'args' => $users_controller->get_collection_params() |
| 186 | ) ); |
| 187 | |
| 188 | register_rest_route( 'writesonic/v2', '/password', array( |
| 189 | 'methods' => WP_REST_Server::CREATABLE, |
| 190 | 'callback' => array( 'WPM_Writesonic_Integration', 'validate_user_password' ), |
| 191 | 'permission_callback' => array( 'WPM_Writesonic_Integration', 'validate_password_permissions_check' ), |
| 192 | 'args' => array( |
| 193 | 'password' => array( |
| 194 | 'default' => null, // значение параметра по умолчанию |
| 195 | 'required' => true, // является ли параметр обязательным. Может быть только true |
| 196 | 'sanitize_callback' => 'sanitize_text_field', // функция очистки значения параметра. Должна вернуть очищенное значение |
| 197 | ) |
| 198 | ) |
| 199 | ) ); |
| 200 | } |
| 201 | |
| 202 | public static function get_user_by_token( $token, $user ) { |
| 203 | $wpb_writesonic_tokens = get_option( WRITESONIC_API_KEY_OPTION ); |
| 204 | |
| 205 | if ( ! is_array( $wpb_writesonic_tokens ) ) { |
| 206 | return $user; |
| 207 | } |
| 208 | |
| 209 | $user_email = array_search( $token, $wpb_writesonic_tokens ); |
| 210 | |
| 211 | if ( $user_email ) { |
| 212 | $user = get_user_by( 'email', $user_email ); |
| 213 | |
| 214 | return $user->ID; |
| 215 | } |
| 216 | |
| 217 | return $user; |
| 218 | } |
| 219 | |
| 220 | public static function get_public_item_schema() { |
| 221 | $posts_controller = new WP_REST_Posts_Controller( 'post' ); |
| 222 | |
| 223 | return $posts_controller->get_public_item_schema(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param WP_REST_Request $request |
| 228 | * |
| 229 | * @return bool |
| 230 | */ |
| 231 | public static function get_users_permissions_check( WP_REST_Request $request ) { |
| 232 | return self::checkAPIKeyAuth( $request ); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @param WP_REST_Request $request |
| 237 | * |
| 238 | * @return bool |
| 239 | */ |
| 240 | public static function get_categories_permissions_check( WP_REST_Request $request ) { |
| 241 | return self::checkAPIKeyAuth( $request ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @param WP_REST_Request $request |
| 246 | * |
| 247 | * @return mixed |
| 248 | */ |
| 249 | public static function get_users( WP_REST_Request $request ) { |
| 250 | $controller = new WP_REST_Users_Controller(); |
| 251 | |
| 252 | $response = $controller->get_items( $request ); |
| 253 | |
| 254 | return $response; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @param WP_REST_Request $request |
| 259 | * |
| 260 | * @return mixed |
| 261 | */ |
| 262 | public static function get_categories( WP_REST_Request $request ) { |
| 263 | $controller = new WP_REST_Terms_Controller( 'category' ); |
| 264 | $response = $controller->get_items( $request ); |
| 265 | |
| 266 | return $response; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * @param WP_REST_Request $request |
| 271 | * |
| 272 | * @return mixed |
| 273 | */ |
| 274 | public static function get_posts_permission_check( WP_REST_Request $request ) { |
| 275 | return self::checkAPIKeyAuth( $request ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * @param WP_REST_Request $request |
| 280 | * |
| 281 | * @return mixed |
| 282 | */ |
| 283 | public static function get_comments( WP_REST_Request $request ) { |
| 284 | $controller = new WP_REST_Comments_Controller(); |
| 285 | $response = $controller->get_items( $request ); |
| 286 | |
| 287 | return $response; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param WP_REST_Request $request |
| 292 | * |
| 293 | * @return bool |
| 294 | */ |
| 295 | public static function get_comments_permission_check( WP_REST_Request $request ) { |
| 296 | return self::checkAPIKeyAuth( $request ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Get posts |
| 301 | * |
| 302 | * @param WP_REST_Request $request |
| 303 | * @return string |
| 304 | */ |
| 305 | public static function get_posts( WP_REST_Request $request ) { |
| 306 | if ( isset( $request['post_type'] ) ) { |
| 307 | $controller = new WP_REST_Posts_Controller( sanitize_text_field( $request['post_type'] ) ); |
| 308 | } else { |
| 309 | $controller = new WP_REST_Posts_Controller( 'post' ); |
| 310 | } |
| 311 | |
| 312 | $response = $controller->get_items( $request ); |
| 313 | |
| 314 | return $response; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Check if correct API key provided in request |
| 319 | * |
| 320 | * @param WP_REST_Request $request |
| 321 | * |
| 322 | * @return bool |
| 323 | */ |
| 324 | public static function checkAPIKeyAuth( WP_REST_Request $request ) { |
| 325 | $auth = isset( $_SERVER['HTTP_TOKEN'] ) ? sanitize_text_field( $_SERVER['HTTP_TOKEN'] ) : false; |
| 326 | $writesonic_token_key = get_option( WRITESONIC_API_KEY_OPTION, true ); |
| 327 | |
| 328 | wp_set_current_user(self::get_user_by_token($auth, $user)); |
| 329 | |
| 330 | if ( is_array($writesonic_token_key) && in_array($auth, $writesonic_token_key) ) { |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * @param WP_REST_Request $request |
| 339 | * |
| 340 | * @return bool |
| 341 | */ |
| 342 | public static function create_post_permissions_check( WP_REST_Request $request ) { |
| 343 | return self::checkAPIKeyAuth( $request ); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * @param WP_REST_Request $request |
| 348 | * |
| 349 | * @return mixed |
| 350 | */ |
| 351 | public static function create_post( WP_REST_Request $request ) { |
| 352 | $post_type = 'post'; |
| 353 | if ( isset( $request['post_type'] ) && $request['post_type'] ) { |
| 354 | $post_type = $request['post_type']; |
| 355 | } |
| 356 | |
| 357 | $controller = new WP_REST_Posts_Controller( $post_type ); |
| 358 | $response = $controller->create_item( $request ); |
| 359 | |
| 360 | return $response; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * @param WP_REST_Request $request |
| 365 | * |
| 366 | * @return mixed |
| 367 | */ |
| 368 | public static function get_media_permission_check( WP_REST_Request $request ) { |
| 369 | return self::checkAPIKeyAuth( $request ); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * @param WP_REST_Request $request |
| 374 | * |
| 375 | * @return mixed |
| 376 | */ |
| 377 | public static function get_media( WP_REST_Request $request ) { |
| 378 | $controller = new WP_REST_Attachments_Controller( 'attachment' ); |
| 379 | $response = $controller->get_items( $request ); |
| 380 | |
| 381 | return $response; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * @param WP_REST_Request $request |
| 386 | * |
| 387 | * @return mixed |
| 388 | */ |
| 389 | public static function create_media_permissions_check( WP_REST_Request $request ) { |
| 390 | return self::checkAPIKeyAuth( $request ); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * @param WP_REST_Request $request |
| 395 | * |
| 396 | * @return mixed |
| 397 | */ |
| 398 | public static function create_media( WP_REST_Request $request ) { |
| 399 | $controller = new WP_REST_Attachments_Controller( 'attachment' ); |
| 400 | $response = $controller->create_item( $request ); |
| 401 | |
| 402 | return $response; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * @param WP_REST_Request $request |
| 407 | * |
| 408 | * @return mixed |
| 409 | */ |
| 410 | public static function create_comment_permissions_check( WP_REST_Request $request ) { |
| 411 | return self::checkAPIKeyAuth( $request ); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @param WP_REST_Request $request |
| 416 | * |
| 417 | * @return mixed |
| 418 | */ |
| 419 | public static function create_comment( WP_REST_Request $request ) { |
| 420 | $controller = new WP_REST_Comments_Controller(); |
| 421 | $response = $controller->create_item( $request ); |
| 422 | |
| 423 | return $response; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * @param WP_REST_Request $request |
| 428 | * |
| 429 | * @return bool |
| 430 | */ |
| 431 | public static function validate_password_permissions_check( WP_REST_Request $request ) { |
| 432 | return self::checkAPIKeyAuth( $request ); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Validate user's password |
| 437 | * |
| 438 | * @param WP_REST_Request $request |
| 439 | * @return string |
| 440 | */ |
| 441 | public static function validate_user_password( WP_REST_Request $request ) { |
| 442 | if ( ! isset( $request['password'] ) || ! isset( $request['user_id'] ) ) { |
| 443 | return []; |
| 444 | } |
| 445 | |
| 446 | $password = $request['password']; |
| 447 | $user = get_user_by( 'id', $request['user_id'] ); |
| 448 | if ( wp_check_password( $password, $user->user_pass ) ) { |
| 449 | $data = array( 'result' => 'true' ); |
| 450 | } else { |
| 451 | $data = array( 'result' => 'false' ); |
| 452 | } |
| 453 | |
| 454 | $response = rest_ensure_response( $data ); |
| 455 | |
| 456 | wp_send_json($response); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Check authorization |
| 461 | * |
| 462 | * @param string $token |
| 463 | * @param string $domain |
| 464 | * @return bool |
| 465 | */ |
| 466 | public static function checkAuthorization( $token, $domain ) { |
| 467 | $body = [ |
| 468 | 'token' => $token, |
| 469 | 'domain' => $domain, |
| 470 | ]; |
| 471 | |
| 472 | $body = wp_json_encode( $body ); |
| 473 | |
| 474 | $options = [ |
| 475 | 'body' => $body, |
| 476 | 'headers' => [ |
| 477 | 'Content-Type' => 'application/json', |
| 478 | ], |
| 479 | 'data_format' => 'body', |
| 480 | ]; |
| 481 | |
| 482 | $request = wp_remote_post( WRITESONIC_CHECK_AUTH_URL, $options ); |
| 483 | |
| 484 | $tmp = json_decode( wp_remote_retrieve_body( $request ), true ); |
| 485 | |
| 486 | return (bool) $tmp; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | WPM_Writesonic_Integration::init(); |
| 491 | } |