get_plugin_url() ) . '&sub=options">' . __( '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() { $pages = Search_Regex_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(); $options = searchregex_get_options(); $versions = array( 'Plugin: ' . SEARCHREGEX_VERSION, 'WordPress: ' . $wp_version . ' (' . ( is_multisite() ? 'multi' : 'single' ) . ')', 'PHP: ' . phpversion(), 'Browser: ' . $this->get_user_agent(), 'JavaScript: ' . plugin_dir_url( SEARCHREGEX_FILE ) . 'search-regex.js', 'REST API: ' . searchregex_get_rest_api(), ); if ( defined( 'SEARCHREGEX_DEV_MODE' ) && SEARCHREGEX_DEV_MODE ) { wp_enqueue_script( 'search-regex', 'http://localhost:3312/search-regex.js', array(), $build, true ); } else { wp_enqueue_script( 'search-regex', plugin_dir_url( SEARCHREGEX_FILE ) . 'search-regex.js', array(), $build, true ); } wp_enqueue_style( 'search-regex', plugin_dir_url( SEARCHREGEX_FILE ) . 'search-regex.css', array(), $build ); wp_localize_script( 'search-regex', 'SearchRegexi10n', array( 'api' => [ 'WP_API_root' => esc_url_raw( searchregex_get_rest_api() ), 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), 'current' => $options['rest_api'], 'routes' => [ SEARCHREGEX_API_JSON => searchregex_get_rest_api( SEARCHREGEX_API_JSON ), SEARCHREGEX_API_JSON_INDEX => searchregex_get_rest_api( SEARCHREGEX_API_JSON_INDEX ), SEARCHREGEX_API_JSON_RELATIVE => searchregex_get_rest_api( SEARCHREGEX_API_JSON_RELATIVE ), ], ], 'pluginBaseUrl' => plugins_url( '', SEARCHREGEX_FILE ), 'pluginRoot' => $this->get_plugin_url(), 'locale' => $this->get_i18n_data(), 'localeSlug' => get_locale(), 'settings' => $options, 'preload' => $preload, 'versions' => implode( "\n", $versions ), 'version' => SEARCHREGEX_VERSION, 'caps' => [ 'pages' => Search_Regex_Capabilities::get_available_pages(), 'capabilities' => Search_Regex_Capabilities::get_all_capabilities(), ], ) ); $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 ) { if ( $api >= 0 && $api <= SEARCHREGEX_API_JSON_RELATIVE ) { searchregex_set_options( array( 'rest_api' => intval( $api, 10 ) ) ); } } /** * Get preloaded data * * @return Array */ private function get_preload_data() { $sources = Source_Manager::get_all_grouped(); $all = Source_Manager::get_all_source_names(); $handler = Source_Manager::get( $all, new Search_Flags(), new Source_Flags() ); $flags = []; foreach ( $handler as $pos => $source ) { $flags[ $all[ $pos ] ] = $source->get_supported_flags(); } return [ 'sources' => $sources, 'source_flags' => $flags, ]; } /** * 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[] = ''; $title = __( 'Search Regex Support', 'search-regex' ); $current_screen = get_current_screen(); if ( $current_screen ) { $current_screen->add_help_tab( array( 'id' => 'search-regex', 'title' => 'Search Regex', 'content' => "

$title

" . implode( "\n", $content ), ) ); } } /** * Get i18n data * * @internal * @return Array */ private function get_i18n_data() { $locale = get_locale(); // WP 4.7 if ( function_exists( 'get_user_locale' ) ) { $locale = get_user_locale(); } $i18n_json = dirname( SEARCHREGEX_FILE ) . '/locale/json/search-regex-' . $locale . '.json'; if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { // phpcs:ignore $locale_data = @file_get_contents( $i18n_json ); if ( $locale_data ) { return json_decode( $locale_data ); } } // Return empty if we have nothing to return so it doesn't fail when parsed in JS return array(); } /** * Admin menu * * @return void */ public function admin_menu() { $hook = add_management_page( 'Search Regex', 'Search Regex', Search_Regex_Capabilities::get_plugin_access(), basename( SEARCHREGEX_FILE ), [ $this, 'admin_screen' ] ); if ( $hook ) { add_action( 'load-' . $hook, [ $this, 'searchregex_head' ] ); } } /** * Check if we meet minimum WP requirements * * @return Bool */ private function check_minimum_wp() { $wp_version = get_bloginfo( 'version' ); if ( version_compare( $wp_version, SEARCHREGEX_MIN_WP, '<' ) ) { return false; } return true; } /** * Admin screen * * @return void */ public function admin_screen() { if ( count( Search_Regex_Capabilities::get_all_capabilities() ) === 0 ) { die( 'You do not have sufficient permissions to access this page.' ); } if ( $this->check_minimum_wp() === false ) { $this->show_minimum_wordpress(); return; } $this->show_main(); } /** * Show minimum supported WP * * @internal * @return void */ private function show_minimum_wordpress() { global $wp_version; /* translators: 1: Expected WordPress version, 2: Actual WordPress version */ $wp_requirement = sprintf( __( 'Search Regex requires WordPress v%1$1s, you are using v%2$2s - please update your WordPress', 'search-regex' ), SEARCHREGEX_MIN_WP, $wp_version ); ?>

show_load_fail(); ?>