*/ protected function input_properties(): array { return array( 'code_type' => array( 'type' => 'string', 'enum' => WPCode_Guard::CODE_TYPES, 'description' => __( 'Only snippets of this code type.', 'acrossai-abilities-manager' ), ), 'active' => array( 'type' => 'boolean', 'description' => __( 'Only active, or only inactive, snippets.', 'acrossai-abilities-manager' ), ), 'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 200, 'default' => 50, 'description' => __( 'Maximum rows to return.', 'acrossai-abilities-manager' ), ), ); } /** * @since 0.0.34 * @return array */ protected function required_input(): array { return array(); } /** * @since 0.0.34 * @return array */ protected function output_properties(): array { return array( 'snippets' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'additionalProperties' => true ), 'description' => __( 'One row per snippet.', 'acrossai-abilities-manager' ), ), 'count' => array( 'type' => 'integer' ), 'safe_mode' => array( 'type' => 'boolean' ), ); } /** * @since 0.0.34 * @return array */ protected function annotations(): array { return array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ); } /** * @since 0.0.34 * @param array $input Input. * @return array|WP_Error */ protected function run( array $input ) { $code_type = isset( $input['code_type'] ) ? (string) $input['code_type'] : ''; if ( '' !== $code_type && ! in_array( $code_type, WPCode_Guard::CODE_TYPES, true ) ) { return new WP_Error( 'invalid_input', sprintf( /* translators: 1: supplied type, 2: accepted list. */ __( '"%1$s" is not a WPCode code type. Accepted types are: %2$s.', 'acrossai-abilities-manager' ), $code_type, implode( ', ', WPCode_Guard::CODE_TYPES ) ) ); } $posts = get_posts( array( 'post_type' => Snippet_Repository::POST_TYPE, 'post_status' => array( 'publish', 'draft' ), 'numberposts' => isset( $input['per_page'] ) ? (int) $input['per_page'] : 50, 'orderby' => 'title', 'order' => 'ASC', 'suppress_filters' => false, ) ); $rows = array(); foreach ( $posts as $post ) { $row = Snippet_Repository::shape( new WPCode_Snippet( $post ) ); if ( '' !== $code_type && $row['code_type'] !== $code_type ) { continue; } if ( array_key_exists( 'active', $input ) && (bool) $input['active'] !== $row['active'] ) { continue; } $rows[] = $row; } return array( 'snippets' => $rows, 'count' => count( $rows ), 'safe_mode' => WPCode_Guard::safe_mode(), ); } }