* @since 3.0.0 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. */ namespace BeyondWords\Editor\Components; /** * Inspect * * @since 3.0.0 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. */ defined( 'ABSPATH' ) || exit; class InspectPanel { /** * Constructor * * @since 6.0.0 Make static. * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. */ public static function init() { add_action( 'add_meta_boxes', [ self::class, 'add_meta_box_callback'] ); add_action( 'rest_api_init', [ self::class, 'rest_api_init_callback'] ); add_filter( 'default_hidden_meta_boxes', [ self::class, 'hide_meta_box'] ); add_action( 'wp_loaded', function (): void { $post_types = \BeyondWords\Settings\Utils::get_compatible_post_types(); if ( is_array( $post_types ) ) { foreach ( $post_types as $post_type ) { add_action( "save_post_{$post_type}", [ self::class, 'save'], 5 ); } } } ); } /** * Hides the metabox by default. * * @since 6.0.0 Make static. * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. * * @param string[] $hidden An array of IDs of meta boxes hidden by default. */ public static function hide_meta_box( $hidden ) { $hidden[] = 'beyondwords__inspect'; return $hidden; } /** * Adds the meta box container for the Classic Editor. * * The Block Editor UI is handled using JavaScript. * * @since 6.0.0 Make static. * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. * * @param string $post_type */ public static function add_meta_box_callback( $post_type ) { $post_types = \BeyondWords\Settings\Utils::get_compatible_post_types(); if ( ! in_array( $post_type, $post_types ) ) { return; } add_meta_box( 'beyondwords__inspect', __( 'BeyondWords', 'speechkit' ) . ': ' . __( 'Inspect', 'speechkit' ), [ self::class, 'render_meta_box_content'], $post_type, 'advanced', 'low', [ '__back_compat_meta_box' => true, ] ); } /** * Render Meta Box content. * * @since 6.0.0 Make static. * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. * * @param \WP_Post $post The post object. */ public static function render_meta_box_content( $post ) { $metadata = \BeyondWords\Post\Meta::get_all_beyondwords_metadata( $post->ID ); self::post_meta_table( $metadata ); ?>
[0-9]+)/content/(?P[a-zA-Z0-9\-]+)', [ // phpcs:ignore Generic.Files.LineLength.TooLong 'methods' => \WP_REST_Server::READABLE, 'callback' => [ self::class, 'rest_api_response'], 'permission_callback' => fn() => current_user_can( 'edit_posts' ), ] ); } /** * Fetches a content object from the BeyondWords REST API. * * @since 6.0.0 Make static. * * @param \WP_REST_Request $request The REST request object. * * @return \WP_REST_Response **/ public static function rest_api_response( \WP_REST_Request $request ) { $project_id = $request['projectId'] ?? ''; $beyondwords_id = $request['beyondwordsId'] ?? ''; // Can be either contentId or sourceId if ( ! is_numeric( $project_id ) ) { return rest_ensure_response( new \WP_Error( 400, __( 'Invalid Project ID', 'speechkit' ), [ 'projectId' => $project_id] ) ); } if ( empty( $beyondwords_id ) ) { return rest_ensure_response( new \WP_Error( 400, __( 'Invalid Content ID', 'speechkit' ), [ 'beyondwordsId' => $beyondwords_id] ) ); } $response = \BeyondWords\Api\Client::get_content( $beyondwords_id, $project_id ); if ( is_wp_error( $response ) ) { return rest_ensure_response( new \WP_Error( 500, __( 'Could not connect to BeyondWords API', 'speechkit' ), $response->get_error_data() ) ); } $code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); if ( $code < 200 || $code >= 300 ) { return rest_ensure_response( new \WP_Error( $code, /* translators: %d is replaced with the error code. */ sprintf( __( 'BeyondWords REST API returned error code %d', 'speechkit' ), $code ), [ 'body' => $body, ] ) ); } $data = json_decode( $body, true ); if ( ! is_array( $data ) ) { return rest_ensure_response( new \WP_Error( 500, __( 'Invalid response from BeyondWords API', 'speechkit' ) ) ); } $data['project_id'] = $project_id; return rest_ensure_response( $data ); } }