prepare_items(); $create_url = \add_query_arg( array( 'page' => Snippets_List::MENU_SLUG, 'action' => 'add', ), self::get_base_admin_url() ); ?>


views(); ?>
search_box( \__( 'Search snippets', '0-day-analytics' ), 'advan-snippets-search' ); ?> display(); ?>
0 ) { $existing = Snippet_Entity::get_snippet( $snippet_id ); if ( empty( $existing ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $existing ); } $now = gmdate( 'Y-m-d H:i:s' ); $slug = Snippet_Entity::build_unique_slug( $name, $snippet_id ?: null ); $data = array( 'id' => $snippet_id, 'blog_id' => \get_current_blog_id(), 'name' => $name, 'slug' => $slug, 'type' => $type, 'status' => $status, 'code' => $code, 'code_hmac' => Snippets_Sandbox::compute_code_hmac( $code ), 'tags' => $tags, 'execution_scope' => $scope, 'execution_hook' => $hook, 'hook_priority' => $priority, 'shortcode_tag' => ( '' === $shortcode_tag ) ? null : $shortcode_tag, 'run_conditions' => $conditions, 'updated_at' => $now, 'created_at' => $existing['created_at'] ?? $now, 'last_run_status' => $existing['last_run_status'] ?? 'never', 'last_run_message' => $existing['last_run_message'] ?? '', 'last_run_at' => $existing['last_run_at'] ?? null, ); if ( 0 === $snippet_id ) { unset( $data['id'] ); } Snippet_Entity::insert( $data ); self::redirect_with_notice( $snippet_id > 0 ? 'updated' : 'created', $snippet_id > 0 ? \__( 'Snippet updated.', '0-day-analytics' ) : \__( 'Snippet created.', '0-day-analytics' ) ); } /** * Handle single delete action. * * @return void * * @since 4.3.0 */ public static function handle_delete(): void { self::enforce_capability(); \check_admin_referer( Snippets_List::NONCE_NAME ); $snippet_id = isset( $_REQUEST['snippet'] ) ? (int) $_REQUEST['snippet'] : 0; if ( $snippet_id <= 0 ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } $snippet = Snippet_Entity::get_snippet( $snippet_id ); if ( empty( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $snippet ); if ( ! Snippet_Entity::is_trashed( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Move the snippet to trash before deleting permanently.', '0-day-analytics' ) ); } Snippet_Entity::delete_by_id( $snippet_id ); self::redirect_with_notice( 'deleted', \__( 'Snippet deleted permanently.', '0-day-analytics' ) ); } /** * Move snippet to trash. * * @return void * * @since 4.3.0 */ public static function handle_trash(): void { self::enforce_capability(); \check_admin_referer( Snippets_List::NONCE_NAME ); $snippet_id = isset( $_REQUEST['snippet'] ) ? (int) $_REQUEST['snippet'] : 0; if ( $snippet_id <= 0 ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } $snippet = Snippet_Entity::get_snippet( $snippet_id ); if ( empty( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $snippet ); if ( Snippet_Entity::is_trashed( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet is already in trash.', '0-day-analytics' ) ); } Snippet_Entity::trash_by_id( $snippet_id ); self::redirect_with_notice( 'trashed', \__( 'Snippet moved to trash.', '0-day-analytics' ) ); } /** * Restore trashed snippet. * * @return void * * @since 4.3.0 */ public static function handle_restore(): void { self::enforce_capability(); \check_admin_referer( Snippets_List::NONCE_NAME ); $snippet_id = isset( $_REQUEST['snippet'] ) ? (int) $_REQUEST['snippet'] : 0; if ( $snippet_id <= 0 ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } $snippet = Snippet_Entity::get_snippet( $snippet_id ); if ( empty( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $snippet ); if ( ! Snippet_Entity::is_trashed( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Only trashed snippets can be restored.', '0-day-analytics' ) ); } Snippet_Entity::restore_by_id( $snippet_id ); self::redirect_with_notice( 'restored', \__( 'Snippet restored.', '0-day-analytics' ) ); } /** * Toggle snippet enabled/disabled status. * * @return void * * @since 4.9.1 */ public static function handle_toggle(): void { self::enforce_capability(); \check_admin_referer( Snippets_List::NONCE_NAME ); $snippet_id = isset( $_REQUEST['snippet'] ) ? (int) $_REQUEST['snippet'] : 0; if ( $snippet_id <= 0 ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } $snippet = Snippet_Entity::get_snippet( $snippet_id ); if ( empty( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $snippet ); if ( Snippet_Entity::is_trashed( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Cannot toggle a trashed snippet.', '0-day-analytics' ) ); } if ( Snippet_Entity::STATUS_ENABLED === (int) $snippet['status'] ) { Snippet_Entity::disable_by_id( $snippet_id ); self::redirect_with_notice( 'toggled', \__( 'Snippet disabled.', '0-day-analytics' ) ); } else { Snippet_Entity::enable_by_id( $snippet_id ); self::redirect_with_notice( 'toggled', \__( 'Snippet enabled.', '0-day-analytics' ) ); } } /** * Duplicate an existing snippet record. * * @return void * * @since 4.3.0 */ public static function handle_clone(): void { self::enforce_capability(); \check_admin_referer( Snippets_List::NONCE_NAME ); $snippet_id = isset( $_REQUEST['snippet'] ) ? (int) $_REQUEST['snippet'] : 0; if ( $snippet_id <= 0 ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } $snippet = Snippet_Entity::get_snippet( $snippet_id ); if ( empty( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $snippet ); $new_id = Snippet_Entity::duplicate( $snippet_id ); if ( null === $new_id ) { self::redirect_with_notice( 'error', \__( 'Could not duplicate snippet.', '0-day-analytics' ) ); } self::redirect_with_notice( 'cloned', \__( 'Snippet duplicated and saved as draft.', '0-day-analytics' ) ); } /** * Execute snippet in sandbox. * * @return void * * @since 4.3.0 */ public static function handle_execute(): void { self::enforce_capability(); \check_admin_referer( Snippets_List::NONCE_NAME ); $snippet_id = isset( $_REQUEST['snippet'] ) ? (int) $_REQUEST['snippet'] : 0; $snippet = ( $snippet_id > 0 ) ? Snippet_Entity::get_snippet( $snippet_id ) : array(); if ( empty( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $snippet ); if ( Snippet_Entity::STATUS_ENABLED !== (int) $snippet['status'] ) { self::redirect_with_notice( 'disabled', \__( 'Enable the snippet before executing it.', '0-day-analytics' ) ); } if ( 'php' !== $snippet['type'] ) { self::redirect_with_notice( 'error', \__( 'Only PHP snippets can be executed from here.', '0-day-analytics' ) ); } $result = Snippets_Sandbox::execute( (string) $snippet['code'], array( 'snippet' => array( 'id' => (int) $snippet['id'], 'name' => $snippet['name'], 'scope' => $snippet['execution_scope'] ?? Snippet_Entity::SCOPE_EVERYWHERE, ), 'conditions' => $snippet['run_conditions'] ?? '', 'trigger' => 'manual', ) ); $message = $result['message'] ?: $result['result_dump']; Snippet_Entity::store_execution_result( (int) $snippet['id'], $result['status'], (string) $message ); $token = \wp_generate_password( 12, false, false ); \set_transient( 'advana_snippet_exec_' . $token, array_merge( $result, array( 'snippet' => array( 'id' => (int) $snippet['id'], 'name' => $snippet['name'], ), ) ), 5 * MINUTE_IN_SECONDS ); self::redirect_with_notice( 'executed', \__( 'Snippet executed.', '0-day-analytics' ), array( 'snippet_token' => $token ) ); } /** * Render add/edit form. * * @param string $action - 'add' or 'edit'. * * @return void * * @since 4.3.0 */ private static function render_form( string $action ): void { $is_edit = ( 'edit' === $action ); $core_title = $is_edit ? \__( 'Edit Snippet', '0-day-analytics' ) : \__( 'Add Snippet', '0-day-analytics' ); $snippet_id = isset( $_GET['snippet'] ) ? (int) $_GET['snippet'] : 0; $snippet = array(); if ( $is_edit ) { $snippet = ( $snippet_id > 0 ) ? Snippet_Entity::get_snippet( $snippet_id ) : array(); if ( empty( $snippet ) ) { self::redirect_with_notice( 'error', \__( 'Snippet not found.', '0-day-analytics' ) ); } self::enforce_blog_scope( $snippet ); } $types = Snippet_Entity::get_supported_types(); $scopes = Snippet_Entity::get_execution_scopes(); $status_value = (int) ( $snippet['status'] ?? 1 ); $status_label = $status_value ? \__( 'Enabled', '0-day-analytics' ) : \__( 'Disabled', '0-day-analytics' ); $status_css = $status_value ? 'is-enabled' : 'is-disabled'; $slug_display = ! empty( $snippet['slug'] ) ? $snippet['slug'] : \__( 'Will be generated on save', '0-day-analytics' ); $last_run_status = ! empty( $snippet['last_run_status'] ) ? $snippet['last_run_status'] : \__( 'Never executed', '0-day-analytics' ); $last_run_time = self::format_relative_time( $snippet['last_run_at'] ?? null ); $created_time = self::format_datetime( $snippet['created_at'] ?? null ); $updated_time = self::format_datetime( $snippet['updated_at'] ?? null ); $scope_value = $snippet['execution_scope'] ?? Snippet_Entity::SCOPE_EVERYWHERE; if ( ! isset( $scopes[ $scope_value ] ) ) { $scope_value = Snippet_Entity::SCOPE_EVERYWHERE; } $hook_value = $snippet['execution_hook'] ?? 'init'; $priority_value = (int) ( $snippet['hook_priority'] ?? 10 ); if ( $priority_value <= 0 ) { $priority_value = 10; } $shortcode_value = $snippet['shortcode_tag'] ?? ''; $conditions_value = $snippet['run_conditions'] ?? ''; $conditions_preview = '' !== $conditions_value ? Snippet_Condition_Evaluator::get_normalized_rules( $conditions_value ) : array(); $execute_url = ''; if ( $is_edit && $snippet_id > 0 && 'php' === ( $snippet['type'] ?? 'php' ) ) { $execute_url = \wp_nonce_url( \add_query_arg( array( 'action' => Snippets_List::EXECUTE_ACTION, 'snippet' => (int) $snippet_id, ), \admin_url( 'admin-post.php' ) ), Snippets_List::NONCE_NAME ); } // Build cancel URL preserving list state (paged, search, filters). $cancel_args = array(); if ( isset( $_REQUEST['paged'] ) ) { $cancel_args['paged'] = (int) $_REQUEST['paged']; } if ( isset( $_REQUEST[ Snippets_List::SEARCH_INPUT ] ) ) { $search = \sanitize_text_field( \wp_unslash( $_REQUEST[ Snippets_List::SEARCH_INPUT ] ) ); if ( '' !== $search ) { $cancel_args[ Snippets_List::SEARCH_INPUT ] = $search; } } if ( isset( $_REQUEST['snippet_status'] ) ) { $status = \sanitize_key( \wp_unslash( $_REQUEST['snippet_status'] ) ); if ( '' !== $status ) { $cancel_args['snippet_status'] = $status; } } if ( isset( $_REQUEST['snippet_type'] ) ) { $type_f = \sanitize_key( \wp_unslash( $_REQUEST['snippet_type'] ) ); if ( '' !== $type_f ) { $cancel_args['snippet_type'] = $type_f; } } $cancel_url = Snippets_List::get_admin_page_url( $cancel_args ); ?>

%2$s

', \esc_attr( $css ), \esc_html( $message ) ); } /** * Rich notice for execution results. * * @param array $data - Execution result data. * * @return void * * @since 4.3.0 */ private static function print_execution_notice( array $data ): void { static $share_script_output = false; $status_class = ( 'success' === $data['status'] ) ? 'notice-success' : 'notice-error'; ?>

Snippets_List::MENU_SLUG, 'snippet_notice' => $code, ); if ( '' !== $message ) { $params['_msg'] = \rawurlencode( $message ); } $status = isset( $_REQUEST['snippet_status'] ) ? \sanitize_key( \wp_unslash( $_REQUEST['snippet_status'] ) ) : ''; if ( '' !== $status && ! isset( $extra['snippet_status'] ) ) { $params['snippet_status'] = $status; } $search = isset( $_REQUEST[ Snippets_List::SEARCH_INPUT ] ) ? \sanitize_text_field( \wp_unslash( $_REQUEST[ Snippets_List::SEARCH_INPUT ] ) ) : ''; if ( '' !== $search && ! isset( $extra[ Snippets_List::SEARCH_INPUT ] ) ) { $params[ Snippets_List::SEARCH_INPUT ] = $search; } if ( ! empty( $extra ) ) { $params = array_merge( $params, $extra ); } $location = \add_query_arg( $params, self::get_base_admin_url() ); \wp_safe_redirect( $location ); exit; } /** * Provide fallback messages for known notice codes. * * @param string $code - Notice code. * * @return string * * @since 4.3.0 */ private static function get_default_notice_message( string $code ): string { $map = array( 'created' => \__( 'Snippet created.', '0-day-analytics' ), 'updated' => \__( 'Snippet updated.', '0-day-analytics' ), 'deleted' => \__( 'Snippet deleted permanently.', '0-day-analytics' ), 'trashed' => \__( 'Snippet moved to trash.', '0-day-analytics' ), 'restored' => \__( 'Snippet restored.', '0-day-analytics' ), 'executed' => \__( 'Snippet executed.', '0-day-analytics' ), 'cloned' => \__( 'Snippet duplicated.', '0-day-analytics' ), ); return $map[ $code ] ?? ''; } /** * Base admin url helper (respects network admin context). * * @return string * * @since 4.3.0 */ private static function get_base_admin_url(): string { return \is_network_admin() ? \network_admin_url( 'admin.php' ) : \admin_url( 'admin.php' ); } } }