jobs = $jobs; } public function register_routes(): void { register_rest_route( self::NAMESPACE, '/jobs', array( array( 'methods' => 'GET', 'callback' => array( $this, 'list' ), 'permission_callback' => array( $this, 'can_manage' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'create' ), 'permission_callback' => array( $this, 'can_manage' ), ), ) ); register_rest_route( self::NAMESPACE, '/jobs/estimate', array( 'methods' => 'POST', 'callback' => array( $this, 'estimate' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); register_rest_route( self::NAMESPACE, '/jobs/(?P\d+)/(?Ppause|resume|cancel)', array( 'methods' => 'POST', 'callback' => array( $this, 'control' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); register_rest_route( self::NAMESPACE, '/jobs/(?P\d+)', array( 'methods' => 'GET', 'callback' => array( $this, 'get' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); register_rest_route( self::NAMESPACE, '/content-types', array( 'methods' => 'GET', 'callback' => array( $this, 'content_types' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); } private function spec( \WP_REST_Request $r ): array { return array( 'post_types' => array_map( 'sanitize_key', (array) ( $r['post_types'] ?: PostTypes::enabled() ) ), 'languages' => array_values( array_intersect( (array) ( $r['languages'] ?: Languages::targets() ), Languages::targets() ) ), 'mode' => in_array( $r['mode'], array( 'missing', 'update', 'all' ), true ) ? $r['mode'] : 'missing', 'include_drafts' => (bool) $r['include_drafts'], ); } public function estimate( \WP_REST_Request $r ): \WP_REST_Response { return rest_ensure_response( $this->jobs->estimate( $this->spec( $r ) ) ); } /** * @return \WP_REST_Response|\WP_Error */ public function create( \WP_REST_Request $r ) { $active = Jobs::active(); if ( $active && 'paused' !== $active['status'] ) { return new \WP_Error( 'translateplus_busy', __( 'A translation job is already running. Pause or cancel it first.', 'translateplus' ), array( 'status' => 409 ) ); } $id = $this->jobs->create( $this->spec( $r ), (int) $r['estimate'] ); return rest_ensure_response( Jobs::get( $id ) ); } public function list(): \WP_REST_Response { return rest_ensure_response( array( 'active' => Jobs::active(), 'recent' => Jobs::recent( 15 ), ) ); } /** * @return \WP_REST_Response|\WP_Error */ public function get( \WP_REST_Request $r ) { $job = Jobs::get( (int) $r['id'] ); return $job ? rest_ensure_response( $job ) : new \WP_Error( 'not_found', __( 'Job not found.', 'translateplus' ), array( 'status' => 404 ) ); } public function control( \WP_REST_Request $r ): \WP_REST_Response { $id = (int) $r['id']; switch ( $r['action'] ) { case 'pause': $this->jobs->pause( $id ); break; case 'resume': $this->jobs->resume( $id ); break; case 'cancel': $this->jobs->cancel( $id ); break; } return rest_ensure_response( Jobs::get( $id ) ); } public function content_types(): \WP_REST_Response { global $wpdb; $counts = array(); foreach ( PostTypes::available() as $name => $label ) { // Original content only: translations are posts too, but are not counted. $count = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prepare( "SELECT COUNT(*) FROM %i p LEFT JOIN %i g ON g.object_type = 'post' AND g.object_id = p.ID WHERE p.post_type = %s AND p.post_status = 'publish' AND ( g.id IS NULL OR g.group_id = g.object_id )", $wpdb->posts, $wpdb->tp_translation_groups, $name ) ); $counts[] = array( 'name' => $name, 'label' => $label, 'count' => $count, 'enabled' => PostTypes::is_translatable( $name ), ); } $taxonomies = array(); foreach ( PostTypes::available_taxonomies() as $name => $label ) { $taxonomies[] = array( 'name' => $name, 'label' => $label, 'enabled' => PostTypes::is_translatable_taxonomy( $name ), ); } return rest_ensure_response( array( 'post_types' => $counts, 'taxonomies' => $taxonomies, ) ); } }