delete(); delete_option( Search\Preset::OPTION_NAME ); } /** * Plugin is activated. Load the presets file * * @return void */ public static function plugin_activated() { } /** * Add plugin settings to plugin page * * @param Array $links Links. * @return Array */ public function plugin_settings( $links ) { array_unshift( $links, '' . __( 'Settings', 'search-regex' ) . '' ); return $links; } /** * Get plugin URL * * @return String */ private function get_plugin_url() { return admin_url( 'tools.php?page=' . basename( SEARCHREGEX_FILE ) ); } /** * Get first page the current user is allowed to see * * @return String */ private function get_first_available_page_url() { /** @psalm-suppress UndefinedClass */ $pages = Plugin\Capabilities::get_available_pages(); if ( count( $pages ) > 0 ) { return $this->get_plugin_url() . ( $pages[0] === 'search' ? '' : '&sub=' . rawurlencode( $pages[0] ) ); } return admin_url(); } /** * Insert stuff into the admin head * * @return void */ public function searchregex_head() { global $wp_version; // Does user have access to this page? if ( $this->get_current_page() === false ) { // Redirect to root plugin page wp_safe_redirect( $this->get_first_available_page_url() ); die(); } if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wp_rest' ) ) { if ( $_REQUEST['action'] === 'rest_api' ) { $this->set_rest_api( intval( $_REQUEST['rest_api'], 10 ) ); } } $build = SEARCHREGEX_VERSION . '-' . SEARCHREGEX_BUILD; $preload = $this->get_preload_data(); $settings = Plugin\Settings::init(); $versions = array( 'Plugin: ' . SEARCHREGEX_VERSION, 'WordPress: ' . $wp_version . ' (' . ( is_multisite() ? 'multi' : 'single' ) . ')', 'PHP: ' . phpversion() . ' ' . ini_get( 'memory_limit' ) . ' ' . ini_get( 'max_execution_time' ) . 's', 'Browser: ' . $this->get_user_agent(), 'JavaScript: ' . plugin_dir_url( SEARCHREGEX_FILE ) . 'search-regex.js', 'REST API: ' . $settings->get_rest_api_url(), ); wp_enqueue_script( 'search-regex', plugin_dir_url( SEARCHREGEX_FILE ) . 'build/search-regex.js', [], $build, true ); wp_enqueue_style( 'search-regex', plugin_dir_url( SEARCHREGEX_FILE ) . 'build/search-regex.css', [], $build ); /** @psalm-suppress UndefinedClass */ $pages = Plugin\Capabilities::get_available_pages(); /** @psalm-suppress UndefinedClass */ $caps = Plugin\Capabilities::get_all_capabilities(); $is_new = false; $major_version = implode( '.', array_slice( explode( '.', SEARCHREGEX_VERSION ), 0, 2 ) ); // phpcs:ignore if ( isset( $_GET['page'] ) && $_GET['page'] === 'search-regex.php' && strpos( SEARCHREGEX_VERSION, '-beta' ) === false ) { $is_new = $settings->is_new_version( $major_version ); } wp_localize_script( 'search-regex', 'SearchRegexi10n', array( 'api' => [ 'WP_API_root' => esc_url_raw( $settings->get_rest_api_url() ), 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), 'site_health' => admin_url( 'site-health.php' ), 'current' => $settings->get_rest_api(), 'routes' => $settings->get_available_rest_api(), ], 'pluginBaseUrl' => plugins_url( '', SEARCHREGEX_FILE ), 'pluginRoot' => $this->get_plugin_url(), 'locale' => implode( '-', array_slice( explode( '-', str_replace( '_', '-', get_locale() ) ), 0, 2 ) ), 'settings' => $settings->get_as_json(), 'preload' => $preload, 'versions' => implode( "\n", $versions ), 'version' => SEARCHREGEX_VERSION, 'caps' => [ 'pages' => $pages, 'capabilities' => $caps, ], 'update_notice' => $is_new ? $major_version : false, ) ); wp_set_script_translations( 'search-regex', 'search-regex', plugin_dir_path( SEARCHREGEX_FILE ) . 'languages/json/' ); $this->add_help_tab(); } /** * Get browser agent * * @return String */ public function get_user_agent() { $agent = ''; if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) { $agent = $_SERVER['HTTP_USER_AGENT']; } return $agent; } /** * Set REST API * * @param Int $api API. * @return void */ private function set_rest_api( $api ) { Plugin\Settings::init()->set_rest_api( intval( $api, 10 ) ); } /** * Get preloaded data * * @return Array */ private function get_preload_data() { $schema = Source\Manager::get_schema(); $presets = Search\Preset::get_all(); return [ 'sources' => Source\Manager::get_all_grouped(), 'presets' => $presets, 'schema' => $schema, 'labels' => $this->get_preload_labels( $presets ), ]; } /** * Get the preloaded labels. * * @param array $presets * @return array */ private function get_preload_labels( array $presets ) { $preload = []; $filters_to_preload = []; if ( isset( $_GET['filters'] ) ) { $filters = json_decode( stripslashes( $_GET['filters'] ), true ); $filters_to_preload = array_merge( $filters_to_preload, $filters ); } foreach ( $presets as $preset ) { if ( isset( $preset['search']['filters'] ) ) { $filters_to_preload = array_merge( $filters_to_preload, $preset['search']['filters'] ); } if ( isset( $preset['search']['action'] ) && $preset['search']['action'] === 'modify' && is_array( $preset['search']['actionOption'] ) ) { $filters_to_preload = array_merge( $filters_to_preload, array_map( function( $action ) { return [ 'type' => $action['source'], 'items' => [ array_merge( [ 'column' => $action['column'] ], $action ), ], ]; }, $preset['search']['actionOption'] ) ); } } foreach ( $filters_to_preload as $filter ) { if ( is_array( $filter ) && isset( $filter['type'] ) && isset( $filter['items'] ) ) { foreach ( $filter['items'] as $filt ) { $preload = array_merge( $preload, Source\Manager::get_schema_preload( $filter['type'], $filt ) ); } } } return array_values( array_filter( $preload ) ); } /** * Replace links * * @internal * @param String $text Text to replace. * @param String $url URL to insert into the link. * @param String $link Link name. * @return String */ private function linkify( $text, $url, $link = 'link' ) { return preg_replace( '@{{' . $link . '}}(.*?){{/' . $link . '}}@', '$1', $text ); } /** * Add help tab * * @internal * @return void */ private function add_help_tab() { $flags = $this->linkify( $this->linkify( __( '{{link}}Source Flags{{/link}} - additional options for the selected source. For example, include post {{guid}}GUID{{/guid}} in the search.', 'search-regex' ), 'https://searchregex.com/support/search-source/' ), 'https://deliciousbrains.com/wordpress-post-guids-sometimes-update/', 'guid' ); /* translators: URL */ $content = [ '
' . sprintf( __( 'You can find full documentation about using Search Regex on the searchregex.com support site.', 'search-regex' ), 'https://searchregex.com/support/' ) . '
' ]; $content[] = '' . __( 'The following concepts are used by Search Regex:', 'search-regex' ) . '
'; $content[] = '