posts = $posts; $this->settings = $settings; } public function register_routes(): void { $routes = array( '/tools/export' => array( 'POST', 'export', 'can_lang' ), '/tools/import' => array( 'POST', 'import', 'can_lang' ), '/tools/migration' => array( 'GET', 'migration_sources', 'can_manage' ), '/tools/migrate' => array( 'POST', 'migrate', 'can_manage' ), '/tools/logs' => array( 'GET', 'logs', 'can_manage' ), '/tools/logs/clear' => array( 'POST', 'clear_logs', 'can_manage' ), '/tools/memory' => array( 'GET', 'memory', 'can_manage' ), '/tools/memory/clear' => array( 'POST', 'clear_memory', 'can_manage' ), '/tools/system' => array( 'GET', 'system', 'can_manage' ), ); foreach ( $routes as $path => list( $method, $cb, $perm ) ) { register_rest_route( self::NAMESPACE, $path, array( 'methods' => $method, 'callback' => array( $this, $cb ), 'permission_callback' => array( $this, $perm ), ) ); } } public function can_lang( \WP_REST_Request $r ): bool { return \TranslatePlus\Workflow\Roles::can_translate( (string) $r['lang'] ); } /** * @return \WP_REST_Response|\WP_Error */ public function export( \WP_REST_Request $r ) { $lang = (string) $r['lang']; if ( ! Languages::is_target( $lang ) ) { return new \WP_Error( 'translateplus_lang', __( 'Choose a language.', 'translateplus' ), array( 'status' => 400 ) ); } $x = new Xliff( $this->posts ); $ids = array_map( 'intval', (array) $r['ids'] ); $types = array_map( 'sanitize_key', (array) $r['post_types'] ); $missing = (bool) $r['only_missing']; $format = 'csv' === $r['format'] ? 'csv' : 'xliff'; $content = 'csv' === $format ? $x->export_csv( $lang, $ids, $types, $missing ) : $x->export_xliff( $lang, $ids, $types, $missing ); return rest_ensure_response( array( 'filename' => sprintf( '%s-%s-%s.%s', sanitize_title( get_bloginfo( 'name' ) ) ?: 'site', Languages::slug( $lang ), gmdate( 'Ymd' ), 'csv' === $format ? 'csv' : 'xliff' ), 'mime' => 'csv' === $format ? 'text/csv' : 'application/x-xliff+xml', 'content' => $content, ) ); } /** * @return \WP_REST_Response|\WP_Error */ public function import( \WP_REST_Request $r ) { $lang = (string) $r['lang']; if ( ! Languages::is_target( $lang ) ) { return new \WP_Error( 'translateplus_lang', __( 'Choose a language.', 'translateplus' ), array( 'status' => 400 ) ); } $content = (string) $r->get_param( 'content' ); if ( strlen( $content ) > 20 * MB_IN_BYTES ) { return new \WP_Error( 'translateplus_size', __( 'File is too large (20 MB maximum).', 'translateplus' ), array( 'status' => 413 ) ); } return rest_ensure_response( ( new Xliff( $this->posts ) )->import( $content, $lang ) ); } public function migration_sources(): \WP_REST_Response { return rest_ensure_response( ( new Migration( $this->settings ) )->detect() ); } /** * @return \WP_REST_Response|\WP_Error */ public function migrate( \WP_REST_Request $r ) { try { $result = ( new Migration( $this->settings ) )->run( sanitize_key( (string) $r['source'] ) ); } catch ( \Throwable $e ) { return new \WP_Error( 'translateplus_migration', $e->getMessage(), array( 'status' => 400 ) ); } return rest_ensure_response( $result ); } public function logs( \WP_REST_Request $r ): \WP_REST_Response { return rest_ensure_response( Logger::recent( 200, sanitize_key( (string) $r['level'] ) ) ); } public function clear_logs(): \WP_REST_Response { Logger::clear(); return rest_ensure_response( array( 'ok' => true ) ); } public function memory(): \WP_REST_Response { return rest_ensure_response( Memory::stats() ); } public function clear_memory(): \WP_REST_Response { Memory::clear(); return rest_ensure_response( Memory::stats() ); } public function system(): \WP_REST_Response { global $wpdb; $pending = function_exists( 'as_get_scheduled_actions' ) ? count( as_get_scheduled_actions( array( 'group' => 'translateplus', 'status' => \ActionScheduler_Store::STATUS_PENDING, 'per_page' => 500 ), 'ids' ) ) : 0; return rest_ensure_response( array( 'plugin_version' => TRANSLATEPLUS_VERSION, 'db_version' => get_option( 'translateplus_db_version' ), 'wp_version' => get_bloginfo( 'version' ), 'php_version' => PHP_VERSION, 'mysql_version' => $wpdb->db_version(), 'multisite' => is_multisite(), 'permalinks' => (string) get_option( 'permalink_structure' ) ?: 'plain', 'url_mode' => $this->settings->get( 'url_mode' ), 'sodium' => function_exists( 'sodium_crypto_secretbox' ), 'object_cache' => wp_using_ext_object_cache(), 'queue_pending' => $pending, 'cron_disabled' => defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON, 'integrations' => array( 'woocommerce' => class_exists( 'WooCommerce' ), 'elementor' => defined( 'ELEMENTOR_VERSION' ), 'bricks' => defined( 'BRICKS_VERSION' ), 'divi' => function_exists( 'et_setup_theme' ) || defined( 'ET_BUILDER_VERSION' ), 'acf' => class_exists( 'ACF' ), 'metabox' => function_exists( 'rwmb_meta' ), 'yoast' => defined( 'WPSEO_VERSION' ), 'rankmath' => class_exists( 'RankMath' ), 'seopress' => defined( 'SEOPRESS_VERSION' ), ), ) ); } }