" }: restores that run and turns theme-level fixes off. * * Accessible copies of PDFs (0.5.0 and later), replaced at the same address; see Alexibility_Pdfs: * POST alexibility/v1/pdfs/preview { "documents": [ { id, url, sha256, fixed, size } ] }: what would happen. * POST alexibility/v1/pdfs/replace ?url=&sha256=&id=&run= with the PDF as the body: one file. The first * call starts a run; pass its id on the next ones. * POST alexibility/v1/pdfs/rollback { "run": "" }: puts that run's originals back. * * @package Alexibility_Fixes */ defined( 'ABSPATH' ) || exit; class Alexibility_Rest { const NS = 'alexibility/v1'; public static function init() { add_action( 'rest_api_init', array( __CLASS__, 'routes' ) ); } public static function allowed() { return current_user_can( Alexibility_Admin::CAP ); } public static function routes() { $routes = array( '/status' => array( 'GET', 'status' ), '/preview' => array( 'POST', 'preview' ), '/apply' => array( 'POST', 'apply' ), '/rollback' => array( 'POST', 'rollback' ), '/pdfs/preview' => array( 'POST', 'pdf_preview' ), '/pdfs/replace' => array( 'POST', 'pdf_replace' ), '/pdfs/rollback' => array( 'POST', 'pdf_rollback' ), ); foreach ( $routes as $route => list( $method, $callback ) ) { register_rest_route( self::NS, $route, array( 'methods' => $method, 'callback' => array( __CLASS__, $callback ), 'permission_callback' => array( __CLASS__, 'allowed' ), ) ); } } public static function status() { // The workbench asks for the status as soon as it connects: from then on this is a client's site, so the // plugin's page waits for the fixes instead of offering a free scan. if ( ! get_option( Alexibility_Admin::CONNECTED ) ) { update_option( Alexibility_Admin::CONNECTED, '1' ); delete_option( Alexibility_Admin::WELCOME ); } $plan = Alexibility_Plan::get(); return array( 'version' => ALEXIBILITY_FIXES_VERSION, 'renderOn' => Alexibility_Render::enabled(), 'badgeOn' => Alexibility_Render::badge_enabled(), 'plan' => $plan ? array( 'origin' => $plan['origin'], 'fixes' => count( $plan['fixes'] ), 'uploaded' => $plan['uploaded'] ) : null, 'runs' => array_slice( (array) get_option( Alexibility_Content::RUNS, array() ), 0, 10 ), 'pdfRuns' => array_slice( (array) get_option( Alexibility_Pdfs::RUNS, array() ), 0, 10 ), 'maxUpload' => (int) wp_max_upload_size(), ); } /** The request body as a validated plan, or the reason it isn't one. */ private static function plan_from( WP_REST_Request $request ) { $body = $request->get_body(); if ( strlen( $body ) > 5 * MB_IN_BYTES ) { return new WP_Error( 'alexibility_fixes_too_big', __( 'The fixes are larger than 5 MB.', 'alexibility-fixes' ), array( 'status' => 413 ) ); } $plan = Alexibility_Plan::from_json( $body ); if ( is_wp_error( $plan ) ) { $plan->add_data( array( 'status' => 400 ) ); } return $plan; } public static function preview( WP_REST_Request $request ) { $plan = self::plan_from( $request ); if ( is_wp_error( $plan ) ) { return $plan; } $changes = Alexibility_Content::plan_changes( $plan['fixes'] ); return array( 'fixes' => array_column( $plan['fixes'], 'id' ), 'skipped' => $plan['skipped'], 'results' => (object) $changes['results'], 'media' => count( $changes['media'] ), 'posts' => count( $changes['posts'] ), ); } public static function apply( WP_REST_Request $request ) { $plan = self::plan_from( $request ); if ( is_wp_error( $plan ) ) { return $plan; } Alexibility_Plan::save( $plan ); $run = Alexibility_Content::apply( $plan['fixes'] ); update_option( Alexibility_Render::ENABLED, true ); return array( 'run' => $run['id'], 'fixes' => array_column( $plan['fixes'], 'id' ), 'skipped' => $plan['skipped'], 'results' => (object) $run['results'], 'changes' => count( $run['changes'] ), 'failed' => (object) $run['failed'], 'failedFixes' => $run['failed_fixes'], // Purged once this request ends; see Alexibility_Cache. 'caches' => Alexibility_Cache::present(), ); } public static function rollback( WP_REST_Request $request ) { $result = Alexibility_Content::rollback( (string) $request->get_param( 'run' ) ); if ( is_wp_error( $result ) ) { $result->add_data( array( 'status' => 404 ) ); return $result; } update_option( Alexibility_Render::ENABLED, false ); $result['caches'] = Alexibility_Cache::present(); return $result; } public static function pdf_preview( WP_REST_Request $request ) { $docs = $request->get_param( 'documents' ); if ( ! is_array( $docs ) || count( $docs ) > 500 ) { return new WP_Error( 'alexibility_fixes_pdf_list', __( 'Send the documents as a list (500 at most).', 'alexibility-fixes' ), array( 'status' => 400 ) ); } return Alexibility_Pdfs::preview( $docs ); } public static function pdf_replace( WP_REST_Request $request ) { return Alexibility_Pdfs::replace( (string) $request->get_param( 'url' ), (string) $request->get_param( 'sha256' ), $request->get_body(), (string) $request->get_param( 'run' ), (string) $request->get_param( 'id' ) ); } public static function pdf_rollback( WP_REST_Request $request ) { return Alexibility_Pdfs::rollback( (string) $request->get_param( 'run' ) ); } }