*/ protected function input_properties(): array { return array( 'id' => array( 'type' => 'integer', 'description' => __( 'Snippet id.', 'acrossai-abilities-manager' ), ), ); } /** * @since 0.0.34 * @return array */ protected function required_input(): array { return array( 'id' ); } /** * @since 0.0.34 * @return array */ protected function output_properties(): array { return array( 'snippet' => array( 'type' => 'object', 'additionalProperties' => true ), 'in_cache' => array( 'type' => 'boolean' ), 'safe_mode' => array( 'type' => 'boolean' ), ); } /** * @since 0.0.34 * @return array */ protected function annotations(): array { return array( 'readonly' => false, 'destructive' => false, 'idempotent' => true ); } /** * @since 0.0.34 * @return bool */ protected function requires_confirmation(): bool { return true; } /** * Only executable snippets need confirming; css and text cannot run anything. * * @since 0.0.34 * @param array $input Input. * @return bool */ protected function needs_confirmation_for( array $input ): bool { $snippet = Snippet_Repository::find( (int) ( $input['id'] ?? 0 ) ); if ( is_wp_error( $snippet ) ) { return false; } return in_array( (string) $snippet->get_code_type(), WPCode_Guard::EXECUTED_TYPES, true ); } /** * @since 0.0.34 * @return string */ protected function confirmation_message(): string { return __( 'This snippet contains code WPCode will execute on the site. Pass confirm: true to switch it on.', 'acrossai-abilities-manager' ); } /** * @since 0.0.34 * @param array $input Input. * @return array|WP_Error */ protected function run( array $input ) { $snippet = Snippet_Repository::find( (int) ( $input['id'] ?? 0 ) ); if ( is_wp_error( $snippet ) ) { return $snippet; } $allowed = WPCode_Guard::assert_code_type( (string) $snippet->get_code_type() ); if ( is_wp_error( $allowed ) ) { return $allowed; } $snippet->active = true; // expect_active: persist() reads the state back and reports activation_refused when // run_activation_checks() rejected the code and WPCode silently saved it switched off. $saved = Snippet_Repository::persist( $snippet, true ); if ( is_wp_error( $saved ) ) { return $saved; } $message = WPCode_Guard::safe_mode() ? __( 'Snippet activated, but safe mode is on for this site so no snippet is executing right now.', 'acrossai-abilities-manager' ) : __( 'Snippet activated and present in the loader cache.', 'acrossai-abilities-manager' ); return array( 'snippet' => $saved, 'in_cache' => $saved['in_cache'], 'safe_mode' => WPCode_Guard::safe_mode(), 'message' => $message, ); } }