id ) { \add_action( 'in_plugin_update_message-' . \ADVAN_PLUGIN_BASENAME, array( __CLASS__, 'prefix_plugin_update_message' ), 10, 2 ); \add_action( 'after_plugin_row_meta', array( __CLASS__, 'after_plugin_row_meta' ), 10, 2 ); // Restrict privileged actions to users who can activate plugins (tighten authorization surface). if ( \current_user_can( 'activate_plugins' ) ) { \add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_custom_wp_admin_style' ) ); if ( true === Settings::get_option( 'show_active_plugins_first' ) ) { \add_action( 'admin_head-plugins.php', array( __CLASS__, 'show_active_plugins_first' ) ); } } } } /** * Shows the message for the upgrading footnotes * * @param array $data - Array with the data. * @param object $response - The response. * * @return void * * @since 1.9.2 */ public static function prefix_plugin_update_message( $data, $response ) { $current_version_parts = explode( '.', \ADVAN_VERSION ); $new_version_parts = explode( '.', $response->new_version ); // If user has already moved to the minor version, we don't need to flag up anything. if ( version_compare( $current_version_parts[0] . '.' . $current_version_parts[1] . '.' . $current_version_parts[2], $new_version_parts[0] . '.' . $new_version_parts[1] . '.' . $new_version_parts[2], '>' ) ) { return; } $upgrade_notice = self::get_upgrade_notice( $response->new_version ); if ( isset( $upgrade_notice ) && ! empty( $upgrade_notice ) ) { printf( '

%s

', $upgrade_notice // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); } } /** * Get the upgrade notice from WordPress.org. * * @param string $version - Plugin new version. * * @return string * * @since 1.9.2 */ private static function get_upgrade_notice( $version ) { // Sanitize version for transient key safety. $sanitized_version = preg_replace( '/[^0-9\\.]/', '', (string) $version ); $transient_name = 'advan_upgrade_notice_' . $sanitized_version; $upgrade_notice = \get_transient( $transient_name ); if ( false === $upgrade_notice || empty( $upgrade_notice ) ) { $response = \wp_safe_remote_get( 'https://plugins.svn.wordpress.org/0-day-analytics/trunk/readme.txt' ); if ( ! \is_wp_error( $response ) && ! empty( $response['body'] ) ) { $upgrade_notice = self::parse_update_notice( $response['body'], $version ); \set_transient( $transient_name, $upgrade_notice, \DAY_IN_SECONDS ); } } return $upgrade_notice; } /** * Parse update notice from readme file. * * @param string $content - Plugin readme file content. * @param string $new_version - Plugin new version. * @return string * * @example readme.txt : * ... * == Upgrade Notice == * * = Version = * Description * ... * * @since 1.9.2 */ private static function parse_update_notice( $content, $new_version ) { $version_parts = explode( '.', $new_version ); $check_for_notices = array( $version_parts[0] . '.' . $version_parts[1] . '.' . $version_parts[2], // Patch. ); // Specify the delimiter in preg_quote to comply with WP coding standards. $notice_regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( $new_version, '~' ) . '\s*=|$)~Uis'; $upgrade_notice = ''; $style = ''; foreach ( $check_for_notices as $check_version ) { if ( version_compare( \ADVAN_VERSION, $check_version, '>' ) ) { continue; } $matches = null; if ( preg_match( $notice_regexp, $content, $matches ) ) { $notices = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) ); if ( version_compare( trim( $matches[1] ), $check_version, '=' ) ) { $style = ''; $upgrade_notice .= '

'; foreach ( $notices as $line ) { // Convert markdown-style links to safe anchors. $upgrade_notice .= preg_replace_callback( '~\\[([^\\]]*)\\]\\(([^\\)]*)\\)~', static function( $m ) { return '' . esc_html( $m[1] ) . ''; }, $line ) . '
'; } $upgrade_notice .= '

'; break; } continue; } } // Final kses pass for safety. $upgrade_notice = wp_kses_post( $upgrade_notice ); return $style . $upgrade_notice; } /** * Adds the file path after the plugin meta. * * @param string $plugin_file Refer to {@see 'plugin_row_meta'} filter. * @param array $plugin_data Refer to {@see 'plugin_row_meta'} filter. * * @return void * * @since 1.9.7 */ public static function after_plugin_row_meta( $plugin_file, $plugin_data ) { if ( \current_user_can( 'activate_plugins' ) ) { if ( isset( $plugin_data['slug'] ) && ! empty( $plugin_data['slug'] ) ) { echo '
'; } } // Avoid disclosing absolute server paths; show relative plugin file only. echo '
' . esc_html( $plugin_file ) . '
'; } /** * Extracts the plugin versions from WordPress.org. * * @param string $plugin_slug - The plugin slug to collect versions for. * * @return \WP_Error|string * * @since 1.9.7 */ public static function extract_plugin_versions( string $plugin_slug ) { // Sanitize slug input defensively. $plugin_slug = sanitize_key( $plugin_slug ); if ( ! function_exists( 'plugins_api' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; } $plugin_information = \plugins_api( 'plugin_information', array( 'slug' => esc_html( $plugin_slug ), 'fields' => array( 'version' => true, 'versions' => true, 'contributors' => false, 'short_description' => false, 'description' => false, 'sections' => false, 'screenshots' => false, 'tags' => false, 'donate_link' => false, 'ratings' => false, ), ) ); if ( \is_wp_error( $plugin_information ) ) { if ( 'plugins_api_failed' === $plugin_information->get_error_code() ) { return new \WP_Error( 'plugins_api_failed', __( 'Plugin API information could not be retrieved.', '0-day-analytics' ) ); } return $plugin_information; } $rollback_versions = array(); if ( is_array( $plugin_information->versions ) && ! empty( $plugin_information->versions ) ) { $versions = $plugin_information->versions; usort( $versions, 'version_compare' ); $versions = array_slice( $versions, -( Settings::get_option( 'plugin_version_switch_count' ) ), Settings::get_option( 'plugin_version_switch_count' ), true ); foreach ( $plugin_information->versions as $version => $download_link ) { $key = array_search( $download_link, $versions, true ); if ( false !== $key && 'trunk' !== $version && ! empty( $version ) ) { $rollback_versions[] = $version; } } } $versions = $rollback_versions; $result = null; if ( is_array( $versions ) && ! array_key_exists( 'errors', $versions ) ) { $result = true; } if ( is_object( $versions ) && property_exists( $versions, 'error_data' ) && ! empty( $versions->error_data ) ) { $result = false; } if ( ! $result ) { return new \WP_Error( 'plugin_versions_not_found', __( 'No Versions Found', '0-day-analytics' ) ); } $versions = $versions ? $versions : array(); if ( ! empty( $versions ) ) { if ( ! array_key_exists( 'errors', $versions ) ) { $option_html = "'; foreach ( $versions as $version ) { $option_html .= ''; } return $option_html; } else { return new \WP_Error( 'plugin_not_found', __( 'Plugin is not found in WordPress ORG', '0-day-analytics' ) ); } } else { return new \WP_Error( 'plugin_version_not_found', __( 'No Version Found', '0-day-analytics' ) ); } } /** * Switch to selected plugin version. * * @param string $plugin_slug - The plugin slug. * @param string $version - The version to switch to. * * @return \WP_Error * * @since 1.9.7 */ public static function version_switch( $plugin_slug, $version ) { if ( empty( $version ) ) { return new \WP_Error( 'plugin_version_invalid', __( 'Error occurred, The version selected is invalid. Try selecting different version.', '0-day-analytics' ) ); } // Sanitize inputs. $plugin_slug = sanitize_key( (string) $plugin_slug ); $version = preg_replace( '/[^0-9\.]/', '', (string) $version ); // Validate requested version against available versions. $available = self::extract_plugin_versions( $plugin_slug ); if ( \is_wp_error( $available ) ) { return $available; // Propagate error. } $found = false; if ( is_string( $available ) ) { if ( preg_match_all( '/]*value=["\']([^"\']+)["\']/', $available, $matches ) ) { foreach ( $matches[1] as $opt_version ) { if ( $opt_version === $version ) { $found = true; break; } } } } if ( ! $found ) { return new \WP_Error( 'plugin_version_not_allowed', __( 'Selected version not in allowed list.', '0-day-analytics' ) ); } return self::upgrade( $plugin_slug, $version ); } /** * Upgrades the plugin to a specific version. * * @param string $plugin_name - The plugin slug. * @param string $version - The version to switch to. * * @return bool|\WP_Error * * @since 1.9.7 */ public static function upgrade( string $plugin_name, $version ) { // Sanitize inputs. $plugin_name = \sanitize_key( (string) $plugin_name ); $version = preg_replace( '/[^0-9\.]/', '', (string) $version ); if ( empty( $version ) ) { return new \WP_Error( 'plugin_version_invalid', __( 'Invalid version.', '0-day-analytics' ) ); } // Verify plugin slug exists among installed plugins directories. if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $installed = \get_plugins(); $slug_found = false; foreach ( array_keys( $installed ) as $file ) { if ( dirname( $file ) === $plugin_name ) { $slug_found = true; break; } } if ( ! $slug_found ) { return new \WP_Error( 'plugin_not_installed', __( 'Plugin slug not found among installed plugins.', '0-day-analytics' ) ); } $update_plugins = \get_site_transient( 'update_plugins' ); if ( ! is_object( $update_plugins ) ) { $update_plugins = new \stdClass(); } $plugin_info = new \stdClass(); $plugin_info->new_version = $version; $plugin_info->slug = $plugin_name; $plugin_info->package = sprintf( 'https://downloads.wordpress.org/plugin/%s.%s.zip', $plugin_name, $version ); $update_plugins->response[ $plugin_name ] = $plugin_info; \set_site_transient( 'update_plugins', $update_plugins ); require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; $title = __( 'Version Switching : - ', '0-day-analytics' ); $title = $title . str_replace( '-', ' ', $plugin_name ) . ' v' . $version; $upgrader_args = array( 'url' => 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $plugin_name ), 'plugin' => $plugin_name, 'nonce' => 'upgrade-plugin_' . $plugin_name, 'title' => $title, ); $upgrader = new \Plugin_Upgrader( new \Plugin_Upgrader_Skin( $upgrader_args ) ); return $upgrader->upgrade( $plugin_name ); } /** * Enqueue the custom admin style. * * @param string $hook - The current admin page. * * @return void * * @since 1.9.7 */ public static function load_custom_wp_admin_style( $hook ) { // Use $hook to ensure function parameter is utilized and restrict script enqueue context if needed. if ( 'plugins.php' !== $hook ) { return; // Only output on plugins screen. } ?> items ) ) { uksort( $wp_list_table->items, array( __CLASS__, 'plugins_order_callback' ) ); } } } /** * Sorts plugins based on their active status * * @param string $a - First plugin. * @param string $b - Second plugin. * * @return int|bool * * @since 3.6.1 */ public static function plugins_order_callback( $a, $b ) { global $wp_list_table; $items = $wp_list_table->items; $a_active = \is_plugin_active( $a ); $b_active = \is_plugin_active( $b ); if ( $a_active && ! $b_active ) { return -1; } elseif ( ! $a_active && $b_active ) { return 1; } elseif ( isset( $items[ $a ] ) && isset( $items[ $b ] ) ) { return strcasecmp( $items[ $a ]['Name'], $items[ $b ]['Name'] ); } return 0; } } }