search-regex
Last commit date
build
1 week ago
includes
1 week ago
changelog.txt
2 years ago
license.txt
18 years ago
readme.txt
1 week ago
search-regex-loader.php
6 months ago
search-regex.php
1 week ago
search-regex.php
121 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Search Regex |
| 4 | Plugin URI: https://searchregex.com/ |
| 5 | Description: Adds search and replace functionality across posts, pages, comments, and meta-data, with full regular expression support |
| 6 | Version: 3.4.3 |
| 7 | Author: John Godley |
| 8 | Requires PHP: 7.4 |
| 9 | Requires at least: 6.6 |
| 10 | Text Domain: search-regex |
| 11 | ============================================================================================================ |
| 12 | For full license details see license.txt |
| 13 | ============================================================================================================ |
| 14 | */ |
| 15 | |
| 16 | define( 'SEARCHREGEX_FILE', __FILE__ ); |
| 17 | |
| 18 | // This file must support PHP < 7.4 so as not to crash |
| 19 | if ( version_compare( phpversion(), '7.4' ) < 0 ) { |
| 20 | // @phpstan-ignore-next-line |
| 21 | add_filter( 'plugin_action_links_' . basename( dirname( SEARCHREGEX_FILE ) ) . '/' . basename( SEARCHREGEX_FILE ), 'searchregex_deprecated_php', 10, 4 ); |
| 22 | |
| 23 | /** |
| 24 | * Show a deprecated PHP warning in the plugin page |
| 25 | * |
| 26 | * @param string[] $links Plugin links. |
| 27 | * @return string[] |
| 28 | */ |
| 29 | function searchregex_deprecated_php( $links ) { |
| 30 | /* translators: 1: server PHP version. 2: required PHP version. */ |
| 31 | array_unshift( $links, '<a href="https://searchregex.com/support/problems/php-version/" style="color: red; text-decoration: underline">' . sprintf( __( 'Disabled! Detected PHP %1$s, need PHP %2$s+', 'search-regex' ), phpversion(), '7.4' ) . '</a>' ); |
| 32 | return $links; |
| 33 | } |
| 34 | |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | spl_autoload_register( |
| 39 | function ( $requested_class ) { |
| 40 | $prefix = 'SearchRegex\\'; |
| 41 | |
| 42 | if ( strncmp( $prefix, $requested_class, strlen( $prefix ) ) !== 0 ) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | $relative_class = substr( $requested_class, strlen( $prefix ) ); |
| 47 | if ( $relative_class === '' ) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | $normalize = static function ( string $value ): string { |
| 52 | return str_replace( '_', '-', strtolower( $value ) ); |
| 53 | }; |
| 54 | |
| 55 | $segments = explode( '\\', $relative_class ); |
| 56 | $class_name = array_pop( $segments ); |
| 57 | $normalized_segments = array_filter( |
| 58 | array_map( $normalize, $segments ), |
| 59 | static fn ( $value ) => $value !== '' |
| 60 | ); |
| 61 | |
| 62 | $base_dir = __DIR__ . '/includes/'; |
| 63 | if ( count( $normalized_segments ) > 0 ) { |
| 64 | $base_dir .= implode( '/', $normalized_segments ) . '/'; |
| 65 | } |
| 66 | |
| 67 | $path = $base_dir . 'class-' . $normalize( $class_name ) . '.php'; |
| 68 | |
| 69 | if ( file_exists( $path ) ) { |
| 70 | require_once $path; |
| 71 | } |
| 72 | } |
| 73 | ); |
| 74 | |
| 75 | // Check if the version is defined. This is to help with mid-update errors. |
| 76 | if ( file_exists( __DIR__ . '/build/search-regex-version.php' ) ) { |
| 77 | require_once __DIR__ . '/build/search-regex-version.php'; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Clear PHP opcache when plugin is updated. This is to help with mid-update errors. |
| 82 | * |
| 83 | * @param object $upgrader The upgrader object. |
| 84 | * @param array{action: string, type: string, plugins?: string[]} $options The upgrade options. |
| 85 | * @return void |
| 86 | */ |
| 87 | function searchregex_clear_opcache_on_upgrade( $upgrader, $options ) { |
| 88 | if ( $options['action'] !== 'update' || $options['type'] !== 'plugin' ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $plugin_basename = plugin_basename( SEARCHREGEX_FILE ); |
| 93 | $plugins = $options['plugins'] ?? []; |
| 94 | |
| 95 | if ( ! in_array( $plugin_basename, $plugins, true ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if ( function_exists( 'opcache_reset' ) ) { |
| 100 | opcache_reset(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | add_action( 'upgrader_process_complete', 'searchregex_clear_opcache_on_upgrade', 10, 2 ); |
| 105 | |
| 106 | if ( is_admin() ) { |
| 107 | // Check if it exists. Version 3.3 updated a lot of files and this might stop some mid-update errors. |
| 108 | if ( ! class_exists( SearchRegex\Admin\Admin::class ) ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | SearchRegex\Admin\Admin::init(); |
| 113 | } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 114 | // Trigger autoloader |
| 115 | class_exists( SearchRegex\Cli\Search_Regex_CLI::class ); |
| 116 | } |
| 117 | |
| 118 | add_action( 'rest_api_init', function () { |
| 119 | SearchRegex\Api\Api::init(); |
| 120 | } ); |
| 121 |