PluginProbe
404 Solution / 2.30.0
404 Solution v2.30.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / 404-solution.php

404-solution.php in 404 Solution 2.30.0, at 404-solution.php

169 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Plugin Name: 404 Solution
5 Plugin URI: https://www.ajexperience.com/404-solution/
6 Description: Creates automatic redirects for 404 traffic and page suggestions when matches are not found providing better service to your web visitors
7 Author: Aaron J
8 Author URI: https://www.ajexperience.com/404-solution/
9
10 Version: 2.30.0
11
12 License: GPL2
13 License URI: https://www.gnu.org/licenses/gpl-2.0.html
14 Update URI: https://www.ajexperience.com/404-solution/
15 Domain Path: /languages
16 Text Domain: 404-solution
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 */
32
33 define('ABJ404_PP', 'abj404_solution');
34 define('ABJ404_FILE', __FILE__);
35 define('ABJ404_PATH', plugin_dir_path(ABJ404_FILE));
36 $GLOBALS['abj404_display_errors'] = false;
37 $GLOBALS['abj404_whitelist'] = array('127.0.0.1', '::1', 'localhost',
38 'ajexperience.com', 'www.ajexperience.com');
39
40 $abj404_autoLoaderClassMap = array();
41 function abj404_autoloader($class) {
42
43 // only pay attention if it's for us. don't bother for other things.
44 if (substr($class, 0, 16) == 'ABJ_404_Solution') {
45 global $abj404_autoLoaderClassMap;
46 if (empty($abj404_autoLoaderClassMap)) {
47 foreach (array('includes/php/objs', 'includes/php/wordpress', 'includes/php', 'includes/php',
48 'includes/ajax', 'includes') as $dir) {
49
50 $globInput = ABJ404_PATH . $dir . DIRECTORY_SEPARATOR . '*.php';
51 $files = glob($globInput);
52 foreach ($files as $file) {
53 // /Users/user..../php/Study.php becomes ABJ_FC\Study
54 $pathParts = pathinfo($file);
55 $classNameWhenLoading = 'ABJ_404_Solution_' . $pathParts['filename'];
56 $abj404_autoLoaderClassMap[$classNameWhenLoading] = $file;
57 }
58 }
59 }
60
61 if (array_key_exists($class, $abj404_autoLoaderClassMap)) {
62 require_once $abj404_autoLoaderClassMap[$class];
63 }
64 }
65 }
66 spl_autoload_register('abj404_autoloader');
67
68 // shortcode
69 add_shortcode('abj404_solution_page_suggestions', 'abj404_shortCodeListener');
70 function abj404_shortCodeListener($atts) {
71 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
72 return ABJ_404_Solution_ShortCode::shortcodePageSuggestions($atts);
73 }
74
75 // admin
76 if (is_admin()) {
77 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
78 ABJ_404_Solution_WordPress_Connector::init();
79 ABJ_404_Solution_ViewUpdater::init();
80 }
81
82 // 404
83 add_action('template_redirect', 'abj404_404listener', 9);
84 function abj404_404listener() {
85 // always ignore admin screens and login requests.
86 $isLoginScreen = (false !== stripos(wp_login_url(), $_SERVER['SCRIPT_NAME']));
87 $isCurrentlyViewingAnAdminPage = is_admin();
88 if ($isCurrentlyViewingAnAdminPage || $isLoginScreen) {
89 return;
90 }
91
92 if (!is_404()) {
93 // if we should redirect all requests then don't return.
94 $options = get_option('abj404_settings');
95 $arrayKeyExists = is_array($options) && array_key_exists('redirect_all_requests', $options);
96 if ($arrayKeyExists && $options['redirect_all_requests'] == 1) {
97 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
98 $connector = ABJ_404_Solution_WordPress_Connector::getInstance();
99 $connector->processRedirectAllRequests();
100 return;
101 }
102
103 /** If we're currently redirecting to a custom 404 page and we are about to show page
104 * suggestions then update the URL displayed to the user. */
105 if (array_key_exists('update_suggest_url', $options) &&
106 isset($options['update_suggest_url']) &&
107 $options['update_suggest_url'] == 1) {
108 $cookieName = ABJ404_PP . '_REQUEST_URI';
109 $cookieName .= '_UPDATE_URL';
110 if (isset($_COOKIE[$cookieName]) && !empty($_COOKIE[$cookieName])) {
111 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
112 add_action('wp_head', 'ABJ_404_Solution_ShortCode::updateURLbarIfNecessary');
113 }
114 }
115 }
116 if (!is_404() || is_admin()) {
117 return;
118 }
119
120 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
121 $connector = ABJ_404_Solution_WordPress_Connector::getInstance();
122 return $connector->process404();
123 }
124
125 function abj404_dailyMaintenanceCronJobListener() {
126 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
127 $abj404dao = ABJ_404_Solution_DataAccess::getInstance();
128 $abj404dao->deleteOldRedirectsCron();
129 }
130 function abj404_updateLogsHitsTableListener() {
131 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
132 $abj404dao = ABJ_404_Solution_DataAccess::getInstance();
133 $abj404dao->createRedirectsForViewHitsTable();
134 }
135 function abj404_updatePermalinkCacheListener($maxExecutionTime, $executionCount = 1) {
136 require_once(plugin_dir_path( __FILE__ ) . "includes/Loader.php");
137 $permalinkCache = ABJ_404_Solution_PermalinkCache::getInstance();
138 $permalinkCache->updatePermalinkCache($maxExecutionTime, $executionCount);
139 }
140 add_action('abj404_cleanupCronAction', 'abj404_dailyMaintenanceCronJobListener');
141 add_action('abj404_updateLogsHitsTableAction', 'abj404_updateLogsHitsTableListener');
142 add_action('abj404_updatePermalinkCacheAction', 'abj404_updatePermalinkCacheListener', 10, 2);
143
144 function abj404_getUploadsDir() {
145 // figure out the temp directory location.
146 $uploadsDirArray = wp_upload_dir(null, false);
147 $uploadsDir = $uploadsDirArray['basedir'];
148 $uploadsDir .= DIRECTORY_SEPARATOR . 'temp_' . ABJ404_PP . DIRECTORY_SEPARATOR;
149 return $uploadsDir;
150 }
151
152 /** This only runs after WordPress is done enqueuing scripts. */
153 function abj404_loadSomethingWhenWordPressIsReady() {
154 // make debugging easier on localhost etc
155 $serverName = '(not found)';
156 if (array_key_exists('SERVER_NAME', $_SERVER) && isset($_SERVER['SERVER_NAME'])) {
157 $serverName = $_SERVER['SERVER_NAME'];
158 }
159 $whiteList = $GLOBALS['abj404_whitelist'];
160 $serverNameIsInTheWhiteList = in_array($serverName, $whiteList);
161 $userIsAnAdminUser = function_exists('wp_get_current_user') &&
162 current_user_can('administrator');
163
164 if ($serverNameIsInTheWhiteList && $userIsAnAdminUser) {
165 $GLOBALS['abj404_display_errors'] = true;
166 }
167 }
168 add_action('init', 'abj404_loadSomethingWhenWordPressIsReady');
169