Completed
Pull Request — master (#378)
by Darren
19:13
created
core/services/request/middleware/BotDetector.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -17,23 +17,23 @@
 block discarded – undo
17 17
 class BotDetector extends Middleware
18 18
 {
19 19
 
20
-    /**
21
-     * converts a Request to a Response
22
-     *
23
-     * @param RequestInterface  $request
24
-     * @param ResponseInterface $response
25
-     * @return ResponseInterface
26
-     */
27
-    public function handleRequest(RequestInterface $request, ResponseInterface $response)
28
-    {
29
-        $this->request = $request;
30
-        $this->response = $response;
31
-        /** @var CrawlerDetect $CrawlerDetect */
32
-        $CrawlerDetect = $this->loader->getShared('EventEspressoVendor\Jaybizzle\CrawlerDetect\CrawlerDetect');
33
-        // Check and record the user agent of the current 'visitor'
34
-        $this->request->setIsBot($CrawlerDetect->isCrawler());
35
-        $this->request->setUserAgent($CrawlerDetect->userAgent());
36
-        $this->response = $this->processRequestStack($this->request, $this->response);
37
-        return $this->response;
38
-    }
20
+	/**
21
+	 * converts a Request to a Response
22
+	 *
23
+	 * @param RequestInterface  $request
24
+	 * @param ResponseInterface $response
25
+	 * @return ResponseInterface
26
+	 */
27
+	public function handleRequest(RequestInterface $request, ResponseInterface $response)
28
+	{
29
+		$this->request = $request;
30
+		$this->response = $response;
31
+		/** @var CrawlerDetect $CrawlerDetect */
32
+		$CrawlerDetect = $this->loader->getShared('EventEspressoVendor\Jaybizzle\CrawlerDetect\CrawlerDetect');
33
+		// Check and record the user agent of the current 'visitor'
34
+		$this->request->setIsBot($CrawlerDetect->isCrawler());
35
+		$this->request->setUserAgent($CrawlerDetect->userAgent());
36
+		$this->response = $this->processRequestStack($this->request, $this->response);
37
+		return $this->response;
38
+	}
39 39
 }
Please login to merge, or discard this patch.
core/services/request/middleware/RecommendedVersions.php 2 patches
Indentation   +172 added lines, -172 removed lines patch added patch discarded remove patch
@@ -19,182 +19,182 @@
 block discarded – undo
19 19
 class RecommendedVersions extends Middleware
20 20
 {
21 21
 
22
-    /**
23
-     * converts a Request to a Response
24
-     *
25
-     * @param RequestInterface  $request
26
-     * @param ResponseInterface $response
27
-     * @return ResponseInterface
28
-     * @throws InvalidDataTypeException
29
-     */
30
-    public function handleRequest(RequestInterface $request, ResponseInterface $response)
31
-    {
32
-        $this->request = $request;
33
-        $this->response = $response;
34
-        // check required WP version
35
-        if (! $this->minimumWordPressVersionRequired()) {
36
-            $this->request->unSetRequestParam('activate', true);
37
-            add_action('admin_notices', array($this, 'minimum_wp_version_error'), 1);
38
-            $this->response->terminateRequest();
39
-            $this->response->deactivatePlugin();
40
-        }
41
-        // check recommended PHP version
42
-        if (! $this->minimumPhpVersionRecommended()) {
43
-            $this->displayMinimumRecommendedPhpVersionNotice();
44
-        }
45
-        // upcoming required version
46
-        if (! $this->upcomingRequiredPhpVersion()) {
47
-            $this->displayUpcomingRequiredVersion();
48
-        }
49
-        $this->response = $this->processRequestStack($this->request, $this->response);
50
-        return $this->response;
51
-    }
52
-
53
-
54
-    /**
55
-     * Helper method to assess installed wp version against given values.
56
-     * By default this compares the required minimum version of WP for EE against the installed version of WP
57
-     * Note, $wp_version is the first parameter sent into the PHP version_compare function (what is being checked
58
-     * against) so consider that when sending in your values.
59
-     *
60
-     * @param string $version_to_check
61
-     * @param string $operator
62
-     * @return bool
63
-     */
64
-    public static function compareWordPressVersion($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=')
65
-    {
66
-        global $wp_version;
67
-        return version_compare(
68
-            // first account for wp_version being pre-release
69
-            // (like RC, beta etc) which are usually in the format like 4.7-RC3-39519
70
-            strpos($wp_version, '-') > 0
71
-                ? substr($wp_version, 0, strpos($wp_version, '-'))
72
-                : $wp_version,
73
-            $version_to_check,
74
-            $operator
75
-        );
76
-    }
77
-
78
-
79
-    /**
80
-     * @return boolean
81
-     */
82
-    private function minimumWordPressVersionRequired()
83
-    {
84
-        return RecommendedVersions::compareWordPressVersion();
85
-    }
86
-
87
-
88
-    /**
89
-     * @param string $min_version
90
-     * @return boolean
91
-     */
92
-    private function checkPhpVersion($min_version = EE_MIN_PHP_VER_RECOMMENDED)
93
-    {
94
-        return version_compare(PHP_VERSION, $min_version, '>=') ? true : false;
95
-    }
96
-
97
-
98
-    /**
99
-     * @return boolean
100
-     */
101
-    private function minimumPhpVersionRecommended()
102
-    {
103
-        return $this->checkPhpVersion();
104
-    }
105
-
106
-
107
-    /**
108
-     * @return void
109
-     */
110
-    public function minimumWpVersionError()
111
-    {
112
-        global $wp_version;
113
-        ?>
22
+	/**
23
+	 * converts a Request to a Response
24
+	 *
25
+	 * @param RequestInterface  $request
26
+	 * @param ResponseInterface $response
27
+	 * @return ResponseInterface
28
+	 * @throws InvalidDataTypeException
29
+	 */
30
+	public function handleRequest(RequestInterface $request, ResponseInterface $response)
31
+	{
32
+		$this->request = $request;
33
+		$this->response = $response;
34
+		// check required WP version
35
+		if (! $this->minimumWordPressVersionRequired()) {
36
+			$this->request->unSetRequestParam('activate', true);
37
+			add_action('admin_notices', array($this, 'minimum_wp_version_error'), 1);
38
+			$this->response->terminateRequest();
39
+			$this->response->deactivatePlugin();
40
+		}
41
+		// check recommended PHP version
42
+		if (! $this->minimumPhpVersionRecommended()) {
43
+			$this->displayMinimumRecommendedPhpVersionNotice();
44
+		}
45
+		// upcoming required version
46
+		if (! $this->upcomingRequiredPhpVersion()) {
47
+			$this->displayUpcomingRequiredVersion();
48
+		}
49
+		$this->response = $this->processRequestStack($this->request, $this->response);
50
+		return $this->response;
51
+	}
52
+
53
+
54
+	/**
55
+	 * Helper method to assess installed wp version against given values.
56
+	 * By default this compares the required minimum version of WP for EE against the installed version of WP
57
+	 * Note, $wp_version is the first parameter sent into the PHP version_compare function (what is being checked
58
+	 * against) so consider that when sending in your values.
59
+	 *
60
+	 * @param string $version_to_check
61
+	 * @param string $operator
62
+	 * @return bool
63
+	 */
64
+	public static function compareWordPressVersion($version_to_check = EE_MIN_WP_VER_REQUIRED, $operator = '>=')
65
+	{
66
+		global $wp_version;
67
+		return version_compare(
68
+			// first account for wp_version being pre-release
69
+			// (like RC, beta etc) which are usually in the format like 4.7-RC3-39519
70
+			strpos($wp_version, '-') > 0
71
+				? substr($wp_version, 0, strpos($wp_version, '-'))
72
+				: $wp_version,
73
+			$version_to_check,
74
+			$operator
75
+		);
76
+	}
77
+
78
+
79
+	/**
80
+	 * @return boolean
81
+	 */
82
+	private function minimumWordPressVersionRequired()
83
+	{
84
+		return RecommendedVersions::compareWordPressVersion();
85
+	}
86
+
87
+
88
+	/**
89
+	 * @param string $min_version
90
+	 * @return boolean
91
+	 */
92
+	private function checkPhpVersion($min_version = EE_MIN_PHP_VER_RECOMMENDED)
93
+	{
94
+		return version_compare(PHP_VERSION, $min_version, '>=') ? true : false;
95
+	}
96
+
97
+
98
+	/**
99
+	 * @return boolean
100
+	 */
101
+	private function minimumPhpVersionRecommended()
102
+	{
103
+		return $this->checkPhpVersion();
104
+	}
105
+
106
+
107
+	/**
108
+	 * @return void
109
+	 */
110
+	public function minimumWpVersionError()
111
+	{
112
+		global $wp_version;
113
+		?>
114 114
         <div class="error">
115 115
             <p>
116 116
                 <?php
117
-                printf(
118
-                    __(
119
-                        'We\'re sorry, but Event Espresso requires WordPress version %1$s or greater in order to operate. You are currently running version %2$s.%3$sFor information on how to update your version of WordPress, please go to %4$s.',
120
-                        'event_espresso'
121
-                    ),
122
-                    EE_MIN_WP_VER_REQUIRED,
123
-                    $wp_version,
124
-                    '<br/>',
125
-                    '<a href="http://codex.wordpress.org/Updating_WordPress">http://codex.wordpress.org/Updating_WordPress</a>'
126
-                );
127
-                ?>
117
+				printf(
118
+					__(
119
+						'We\'re sorry, but Event Espresso requires WordPress version %1$s or greater in order to operate. You are currently running version %2$s.%3$sFor information on how to update your version of WordPress, please go to %4$s.',
120
+						'event_espresso'
121
+					),
122
+					EE_MIN_WP_VER_REQUIRED,
123
+					$wp_version,
124
+					'<br/>',
125
+					'<a href="http://codex.wordpress.org/Updating_WordPress">http://codex.wordpress.org/Updating_WordPress</a>'
126
+				);
127
+				?>
128 128
             </p>
129 129
         </div>
130 130
         <?php
131
-    }
132
-
133
-
134
-    /**
135
-     *    _display_minimum_recommended_php_version_notice
136
-     *
137
-     * @access private
138
-     * @return void
139
-     * @throws InvalidDataTypeException
140
-     */
141
-    private function displayMinimumRecommendedPhpVersionNotice()
142
-    {
143
-        if ($this->request->isAdmin()) {
144
-            new PersistentAdminNotice(
145
-                'php_version_' . str_replace('.', '-', EE_MIN_PHP_VER_RECOMMENDED) . '_recommended',
146
-                sprintf(
147
-                    esc_html__(
148
-                        'Event Espresso recommends PHP version %1$s or greater for optimal performance. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
149
-                        'event_espresso'
150
-                    ),
151
-                    EE_MIN_PHP_VER_RECOMMENDED,
152
-                    PHP_VERSION,
153
-                    '<br/>',
154
-                    '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
155
-                )
156
-            );
157
-        }
158
-    }
159
-
160
-
161
-    /**
162
-     * Returns whether the provided php version number is less than the current version of php installed on the server.
163
-     *
164
-     * @param string $version_required
165
-     * @return bool
166
-     */
167
-    private function upcomingRequiredPhpVersion($version_required = '5.5')
168
-    {
169
-        return true;
170
-        // return $this->checkPhpVersion($version_required);
171
-    }
172
-
173
-
174
-    /**
175
-     *  Sets a notice for an upcoming required version of PHP in the next update of EE core.
176
-     */
177
-    private function displayUpcomingRequiredVersion()
178
-    {
179
-        if ($this->request->isAdmin()
180
-            && apply_filters('FHEE__EE_Recommended_Versions__displayUpcomingRequiredVersion', true, $this->request)
181
-            && current_user_can('update_plugins')
182
-        ) {
183
-            add_action('admin_notices', function () {
184
-                echo '<div class="notice event-espresso-admin-notice notice-warning"><p>'
185
-                     . sprintf(
186
-                         esc_html__(
187
-                             'Please note: The next update of Event Espresso 4 will %1$srequire%2$s PHP 5.4.45 or greater.  Your web server\'s PHP version is %3$s.  You can contact your host and ask them to update your PHP version to at least PHP 5.6.  Please do not update to the new version of Event Espresso 4 until the PHP update is completed. Read about why keeping your server on the latest version of PHP is a good idea %4$shere%5$s',
188
-                             'event_espresso'
189
-                         ),
190
-                         '<strong>',
191
-                         '</strong>',
192
-                         PHP_VERSION,
193
-                         '<a href="https://wordpress.org/support/upgrade-php/">',
194
-                         '</a>'
195
-                     )
196
-                     . '</p></div>';
197
-            });
198
-        }
199
-    }
131
+	}
132
+
133
+
134
+	/**
135
+	 *    _display_minimum_recommended_php_version_notice
136
+	 *
137
+	 * @access private
138
+	 * @return void
139
+	 * @throws InvalidDataTypeException
140
+	 */
141
+	private function displayMinimumRecommendedPhpVersionNotice()
142
+	{
143
+		if ($this->request->isAdmin()) {
144
+			new PersistentAdminNotice(
145
+				'php_version_' . str_replace('.', '-', EE_MIN_PHP_VER_RECOMMENDED) . '_recommended',
146
+				sprintf(
147
+					esc_html__(
148
+						'Event Espresso recommends PHP version %1$s or greater for optimal performance. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
149
+						'event_espresso'
150
+					),
151
+					EE_MIN_PHP_VER_RECOMMENDED,
152
+					PHP_VERSION,
153
+					'<br/>',
154
+					'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
155
+				)
156
+			);
157
+		}
158
+	}
159
+
160
+
161
+	/**
162
+	 * Returns whether the provided php version number is less than the current version of php installed on the server.
163
+	 *
164
+	 * @param string $version_required
165
+	 * @return bool
166
+	 */
167
+	private function upcomingRequiredPhpVersion($version_required = '5.5')
168
+	{
169
+		return true;
170
+		// return $this->checkPhpVersion($version_required);
171
+	}
172
+
173
+
174
+	/**
175
+	 *  Sets a notice for an upcoming required version of PHP in the next update of EE core.
176
+	 */
177
+	private function displayUpcomingRequiredVersion()
178
+	{
179
+		if ($this->request->isAdmin()
180
+			&& apply_filters('FHEE__EE_Recommended_Versions__displayUpcomingRequiredVersion', true, $this->request)
181
+			&& current_user_can('update_plugins')
182
+		) {
183
+			add_action('admin_notices', function () {
184
+				echo '<div class="notice event-espresso-admin-notice notice-warning"><p>'
185
+					 . sprintf(
186
+						 esc_html__(
187
+							 'Please note: The next update of Event Espresso 4 will %1$srequire%2$s PHP 5.4.45 or greater.  Your web server\'s PHP version is %3$s.  You can contact your host and ask them to update your PHP version to at least PHP 5.6.  Please do not update to the new version of Event Espresso 4 until the PHP update is completed. Read about why keeping your server on the latest version of PHP is a good idea %4$shere%5$s',
188
+							 'event_espresso'
189
+						 ),
190
+						 '<strong>',
191
+						 '</strong>',
192
+						 PHP_VERSION,
193
+						 '<a href="https://wordpress.org/support/upgrade-php/">',
194
+						 '</a>'
195
+					 )
196
+					 . '</p></div>';
197
+			});
198
+		}
199
+	}
200 200
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -32,18 +32,18 @@  discard block
 block discarded – undo
32 32
         $this->request = $request;
33 33
         $this->response = $response;
34 34
         // check required WP version
35
-        if (! $this->minimumWordPressVersionRequired()) {
35
+        if ( ! $this->minimumWordPressVersionRequired()) {
36 36
             $this->request->unSetRequestParam('activate', true);
37 37
             add_action('admin_notices', array($this, 'minimum_wp_version_error'), 1);
38 38
             $this->response->terminateRequest();
39 39
             $this->response->deactivatePlugin();
40 40
         }
41 41
         // check recommended PHP version
42
-        if (! $this->minimumPhpVersionRecommended()) {
42
+        if ( ! $this->minimumPhpVersionRecommended()) {
43 43
             $this->displayMinimumRecommendedPhpVersionNotice();
44 44
         }
45 45
         // upcoming required version
46
-        if (! $this->upcomingRequiredPhpVersion()) {
46
+        if ( ! $this->upcomingRequiredPhpVersion()) {
47 47
             $this->displayUpcomingRequiredVersion();
48 48
         }
49 49
         $this->response = $this->processRequestStack($this->request, $this->response);
@@ -142,7 +142,7 @@  discard block
 block discarded – undo
142 142
     {
143 143
         if ($this->request->isAdmin()) {
144 144
             new PersistentAdminNotice(
145
-                'php_version_' . str_replace('.', '-', EE_MIN_PHP_VER_RECOMMENDED) . '_recommended',
145
+                'php_version_'.str_replace('.', '-', EE_MIN_PHP_VER_RECOMMENDED).'_recommended',
146 146
                 sprintf(
147 147
                     esc_html__(
148 148
                         'Event Espresso recommends PHP version %1$s or greater for optimal performance. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
             && apply_filters('FHEE__EE_Recommended_Versions__displayUpcomingRequiredVersion', true, $this->request)
181 181
             && current_user_can('update_plugins')
182 182
         ) {
183
-            add_action('admin_notices', function () {
183
+            add_action('admin_notices', function() {
184 184
                 echo '<div class="notice event-espresso-admin-notice notice-warning"><p>'
185 185
                      . sprintf(
186 186
                          esc_html__(
Please login to merge, or discard this patch.
core/services/request/middleware/PreProductionVersionWarning.php 1 patch
Indentation   +79 added lines, -79 removed lines patch added patch discarded remove patch
@@ -18,91 +18,91 @@
 block discarded – undo
18 18
 class PreProductionVersionWarning extends Middleware
19 19
 {
20 20
 
21
-    /**
22
-     * converts a Request to a Response
23
-     *
24
-     * @param RequestInterface  $request
25
-     * @param ResponseInterface $response
26
-     * @return ResponseInterface
27
-     */
28
-    public function handleRequest(RequestInterface $request, ResponseInterface $response)
29
-    {
30
-        $this->request = $request;
31
-        $this->response = $response;
32
-        $this->displayPreProductionVersionWarning();
33
-        $this->response = $this->processRequestStack($this->request, $this->response);
34
-        return $this->response;
35
-    }
21
+	/**
22
+	 * converts a Request to a Response
23
+	 *
24
+	 * @param RequestInterface  $request
25
+	 * @param ResponseInterface $response
26
+	 * @return ResponseInterface
27
+	 */
28
+	public function handleRequest(RequestInterface $request, ResponseInterface $response)
29
+	{
30
+		$this->request = $request;
31
+		$this->response = $response;
32
+		$this->displayPreProductionVersionWarning();
33
+		$this->response = $this->processRequestStack($this->request, $this->response);
34
+		return $this->response;
35
+	}
36 36
 
37 37
 
38
-    /**
39
-     * displays message on frontend of site notifying admin that EE has been temporarily placed into maintenance mode
40
-     *
41
-     * @return void
42
-     */
43
-    public function displayPreProductionVersionWarning()
44
-    {
45
-        // skip AJAX requests
46
-        if ($this->request->isAjax()) {
47
-            return;
48
-        }
49
-        // skip stable releases
50
-        if (substr(EVENT_ESPRESSO_VERSION, -5) !== '.beta') {
51
-            return;
52
-        }
53
-        // site admin has authorized use of non-stable release candidate for production
54
-        if (defined('ALLOW_NON_STABLE_RELEASE_ON_LIVE_SITE') && ALLOW_NON_STABLE_RELEASE_ON_LIVE_SITE) {
55
-            return;
56
-        }
57
-        // post release candidate warning
58
-        if ($this->request->isAdmin()) {
59
-            add_action('admin_notices', array($this, 'preProductionVersionAdminNotice'), -999);
60
-        } else {
61
-            add_action('shutdown', array($this, 'preProductionVersionWarningNotice'), 10);
62
-        }
63
-    }
38
+	/**
39
+	 * displays message on frontend of site notifying admin that EE has been temporarily placed into maintenance mode
40
+	 *
41
+	 * @return void
42
+	 */
43
+	public function displayPreProductionVersionWarning()
44
+	{
45
+		// skip AJAX requests
46
+		if ($this->request->isAjax()) {
47
+			return;
48
+		}
49
+		// skip stable releases
50
+		if (substr(EVENT_ESPRESSO_VERSION, -5) !== '.beta') {
51
+			return;
52
+		}
53
+		// site admin has authorized use of non-stable release candidate for production
54
+		if (defined('ALLOW_NON_STABLE_RELEASE_ON_LIVE_SITE') && ALLOW_NON_STABLE_RELEASE_ON_LIVE_SITE) {
55
+			return;
56
+		}
57
+		// post release candidate warning
58
+		if ($this->request->isAdmin()) {
59
+			add_action('admin_notices', array($this, 'preProductionVersionAdminNotice'), -999);
60
+		} else {
61
+			add_action('shutdown', array($this, 'preProductionVersionWarningNotice'), 10);
62
+		}
63
+	}
64 64
 
65 65
 
66
-    /**
67
-     * displays admin notice that current version of EE is not a stable release
68
-     *
69
-     * @return void
70
-     * @throws InvalidDataTypeException
71
-     */
72
-    public function preProductionVersionAdminNotice()
73
-    {
74
-        new PersistentAdminNotice(
75
-            'preProductionVersionAdminNotice_' . EVENT_ESPRESSO_VERSION,
76
-            $this->warningNotice()
77
-        );
78
-    }
66
+	/**
67
+	 * displays admin notice that current version of EE is not a stable release
68
+	 *
69
+	 * @return void
70
+	 * @throws InvalidDataTypeException
71
+	 */
72
+	public function preProductionVersionAdminNotice()
73
+	{
74
+		new PersistentAdminNotice(
75
+			'preProductionVersionAdminNotice_' . EVENT_ESPRESSO_VERSION,
76
+			$this->warningNotice()
77
+		);
78
+	}
79 79
 
80 80
 
81
-    /**
82
-     * displays message on frontend of site notifying admin that current version of EE is not a stable release
83
-     *
84
-     * @return void
85
-     */
86
-    public function preProductionVersionWarningNotice()
87
-    {
88
-        echo '<div id="ee-release-candidate-notice-dv" class="ee-really-important-notice-dv"><p>';
89
-        echo $this->warningNotice();
90
-        echo '</p></div>';
91
-    }
81
+	/**
82
+	 * displays message on frontend of site notifying admin that current version of EE is not a stable release
83
+	 *
84
+	 * @return void
85
+	 */
86
+	public function preProductionVersionWarningNotice()
87
+	{
88
+		echo '<div id="ee-release-candidate-notice-dv" class="ee-really-important-notice-dv"><p>';
89
+		echo $this->warningNotice();
90
+		echo '</p></div>';
91
+	}
92 92
 
93 93
 
94
-    /**
95
-     * @return string
96
-     */
97
-    private function warningNotice()
98
-    {
99
-        return sprintf(
100
-            esc_html__(
101
-                'This version of Event Espresso is for testing and/or evaluation purposes only. It is %1$snot%2$s considered a stable release and should therefore %1$snot%2$s be activated on a live or production website.',
102
-                'event_espresso'
103
-            ),
104
-            '<strong>',
105
-            '</strong>'
106
-        );
107
-    }
94
+	/**
95
+	 * @return string
96
+	 */
97
+	private function warningNotice()
98
+	{
99
+		return sprintf(
100
+			esc_html__(
101
+				'This version of Event Espresso is for testing and/or evaluation purposes only. It is %1$snot%2$s considered a stable release and should therefore %1$snot%2$s be activated on a live or production website.',
102
+				'event_espresso'
103
+			),
104
+			'<strong>',
105
+			'</strong>'
106
+		);
107
+	}
108 108
 }
Please login to merge, or discard this patch.
core/services/request/middleware/DetectLogin.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -16,28 +16,28 @@
 block discarded – undo
16 16
 class DetectLogin extends Middleware
17 17
 {
18 18
 
19
-    /**
20
-     * converts a Request to a Response
21
-     *
22
-     * @param RequestInterface  $request
23
-     * @param ResponseInterface $response
24
-     * @return ResponseInterface
25
-     */
26
-    public function handleRequest(RequestInterface $request, ResponseInterface $response)
27
-    {
28
-        $this->request = $request;
29
-        $this->response = $response;
30
-        global $pagenow;
31
-        if (in_array(
32
-            $pagenow,
33
-            array('wp-login.php', 'wp-register.php'),
34
-            true
35
-        )
36
-            && ! filter_var($request->getRequestParam('ee_load_on_login'), FILTER_VALIDATE_BOOLEAN)
37
-        ) {
38
-            $this->response->terminateRequest();
39
-        }
40
-        $this->response = $this->processRequestStack($this->request, $this->response);
41
-        return $this->response;
42
-    }
19
+	/**
20
+	 * converts a Request to a Response
21
+	 *
22
+	 * @param RequestInterface  $request
23
+	 * @param ResponseInterface $response
24
+	 * @return ResponseInterface
25
+	 */
26
+	public function handleRequest(RequestInterface $request, ResponseInterface $response)
27
+	{
28
+		$this->request = $request;
29
+		$this->response = $response;
30
+		global $pagenow;
31
+		if (in_array(
32
+			$pagenow,
33
+			array('wp-login.php', 'wp-register.php'),
34
+			true
35
+		)
36
+			&& ! filter_var($request->getRequestParam('ee_load_on_login'), FILTER_VALIDATE_BOOLEAN)
37
+		) {
38
+			$this->response->terminateRequest();
39
+		}
40
+		$this->response = $this->processRequestStack($this->request, $this->response);
41
+		return $this->response;
42
+	}
43 43
 }
Please login to merge, or discard this patch.
core/services/request/middleware/Middleware.php 2 patches
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -25,52 +25,52 @@
 block discarded – undo
25 25
 abstract class Middleware implements RequestDecoratorInterface
26 26
 {
27 27
 
28
-    /**
29
-     * @var RequestDecoratorInterface $request_stack_app
30
-     */
31
-    protected $request_stack_app;
28
+	/**
29
+	 * @var RequestDecoratorInterface $request_stack_app
30
+	 */
31
+	protected $request_stack_app;
32 32
 
33
-    /**
34
-     * @var RequestInterface $request
35
-     */
36
-    protected $request;
33
+	/**
34
+	 * @var RequestInterface $request
35
+	 */
36
+	protected $request;
37 37
 
38
-    /**
39
-     * @var ResponseInterface $response
40
-     */
41
-    protected $response;
38
+	/**
39
+	 * @var ResponseInterface $response
40
+	 */
41
+	protected $response;
42 42
 
43
-    /**
44
-     * @var LoaderInterface
45
-     */
46
-    protected $loader;
43
+	/**
44
+	 * @var LoaderInterface
45
+	 */
46
+	protected $loader;
47 47
 
48 48
 
49
-    /**
50
-     * @param RequestDecoratorInterface $request_stack_app
51
-     * @param LoaderInterface           $loader
52
-     */
53
-    public function __construct(RequestDecoratorInterface $request_stack_app, LoaderInterface $loader)
54
-    {
55
-        $this->request_stack_app = $request_stack_app;
56
-        $this->loader = $loader;
57
-    }
49
+	/**
50
+	 * @param RequestDecoratorInterface $request_stack_app
51
+	 * @param LoaderInterface           $loader
52
+	 */
53
+	public function __construct(RequestDecoratorInterface $request_stack_app, LoaderInterface $loader)
54
+	{
55
+		$this->request_stack_app = $request_stack_app;
56
+		$this->loader = $loader;
57
+	}
58 58
 
59 59
 
60
-    /**
61
-     * process_request_stack
62
-     *
63
-     * @param RequestInterface  $request
64
-     * @param ResponseInterface $response
65
-     * @return ResponseInterface
66
-     */
67
-    protected function processRequestStack(RequestInterface $request, ResponseInterface $response)
68
-    {
69
-        $this->request = $request;
70
-        $this->response = $response;
71
-        if (! $this->response->requestTerminated()) {
72
-            $this->response = $this->request_stack_app->handleRequest($this->request, $this->response);
73
-        }
74
-        return $this->response;
75
-    }
60
+	/**
61
+	 * process_request_stack
62
+	 *
63
+	 * @param RequestInterface  $request
64
+	 * @param ResponseInterface $response
65
+	 * @return ResponseInterface
66
+	 */
67
+	protected function processRequestStack(RequestInterface $request, ResponseInterface $response)
68
+	{
69
+		$this->request = $request;
70
+		$this->response = $response;
71
+		if (! $this->response->requestTerminated()) {
72
+			$this->response = $this->request_stack_app->handleRequest($this->request, $this->response);
73
+		}
74
+		return $this->response;
75
+	}
76 76
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@
 block discarded – undo
68 68
     {
69 69
         $this->request = $request;
70 70
         $this->response = $response;
71
-        if (! $this->response->requestTerminated()) {
71
+        if ( ! $this->response->requestTerminated()) {
72 72
             $this->response = $this->request_stack_app->handleRequest($this->request, $this->response);
73 73
         }
74 74
         return $this->response;
Please login to merge, or discard this patch.
core/services/request/Request.php 2 patches
Indentation   +573 added lines, -573 removed lines patch added patch discarded remove patch
@@ -16,577 +16,577 @@
 block discarded – undo
16 16
 class Request implements InterminableInterface, RequestInterface
17 17
 {
18 18
 
19
-    /**
20
-     * $_GET parameters
21
-     *
22
-     * @var array $get
23
-     */
24
-    private $get;
25
-
26
-    /**
27
-     * $_POST parameters
28
-     *
29
-     * @var array $post
30
-     */
31
-    private $post;
32
-
33
-    /**
34
-     * $_COOKIE parameters
35
-     *
36
-     * @var array $cookie
37
-     */
38
-    private $cookie;
39
-
40
-    /**
41
-     * $_SERVER parameters
42
-     *
43
-     * @var array $server
44
-     */
45
-    private $server;
46
-
47
-    /**
48
-     * $_REQUEST parameters
49
-     *
50
-     * @var array $request
51
-     */
52
-    private $request;
53
-
54
-    /**
55
-     * @var RequestTypeContextCheckerInterface
56
-     */
57
-    private $request_type;
58
-
59
-    /**
60
-     * IP address for request
61
-     *
62
-     * @var string $ip_address
63
-     */
64
-    private $ip_address;
65
-
66
-    /**
67
-     * @var string $user_agent
68
-     */
69
-    private $user_agent;
70
-
71
-    /**
72
-     * true if current user appears to be some kind of bot
73
-     *
74
-     * @var bool $is_bot
75
-     */
76
-    private $is_bot;
77
-
78
-
79
-    /**
80
-     * @param array $get
81
-     * @param array $post
82
-     * @param array $cookie
83
-     * @param array $server
84
-     */
85
-    public function __construct(array $get, array $post, array $cookie, array $server)
86
-    {
87
-        // grab request vars
88
-        $this->get = $get;
89
-        $this->post = $post;
90
-        $this->cookie = $cookie;
91
-        $this->server = $server;
92
-        $this->request = array_merge($this->get, $this->post);
93
-        $this->ip_address = $this->visitorIp();
94
-    }
95
-
96
-
97
-    /**
98
-     * @param RequestTypeContextCheckerInterface $type
99
-     */
100
-    public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type)
101
-    {
102
-        $this->request_type = $type;
103
-    }
104
-
105
-
106
-    /**
107
-     * @return array
108
-     */
109
-    public function getParams()
110
-    {
111
-        return $this->get;
112
-    }
113
-
114
-
115
-    /**
116
-     * @return array
117
-     */
118
-    public function postParams()
119
-    {
120
-        return $this->post;
121
-    }
122
-
123
-
124
-    /**
125
-     * @return array
126
-     */
127
-    public function cookieParams()
128
-    {
129
-        return $this->cookie;
130
-    }
131
-
132
-
133
-    /**
134
-     * @return array
135
-     */
136
-    public function serverParams()
137
-    {
138
-        return $this->server;
139
-    }
140
-
141
-
142
-    /**
143
-     * returns contents of $_REQUEST
144
-     *
145
-     * @return array
146
-     */
147
-    public function requestParams()
148
-    {
149
-        return $this->request;
150
-    }
151
-
152
-
153
-    /**
154
-     * @param      $key
155
-     * @param      $value
156
-     * @param bool $override_ee
157
-     * @return    void
158
-     */
159
-    public function setRequestParam($key, $value, $override_ee = false)
160
-    {
161
-        // don't allow "ee" to be overwritten unless explicitly instructed to do so
162
-        if ($key !== 'ee'
163
-            || ($key === 'ee' && empty($this->request['ee']))
164
-            || ($key === 'ee' && ! empty($this->request['ee']) && $override_ee)
165
-        ) {
166
-            $this->request[$key] = $value;
167
-        }
168
-    }
169
-
170
-
171
-    /**
172
-     * returns   the value for a request param if the given key exists
173
-     *
174
-     * @param       $key
175
-     * @param null  $default
176
-     * @return mixed
177
-     */
178
-    public function getRequestParam($key, $default = null)
179
-    {
180
-        return $this->requestParameterDrillDown($key, $default, 'get');
181
-    }
182
-
183
-
184
-    /**
185
-     * check if param exists
186
-     *
187
-     * @param       $key
188
-     * @return bool
189
-     */
190
-    public function requestParamIsSet($key)
191
-    {
192
-        return $this->requestParameterDrillDown($key);
193
-    }
194
-
195
-
196
-    /**
197
-     * check if a request parameter exists whose key that matches the supplied wildcard pattern
198
-     * and return the value for the first match found
199
-     * wildcards can be either of the following:
200
-     *      ? to represent a single character of any type
201
-     *      * to represent one or more characters of any type
202
-     *
203
-     * @param string     $pattern
204
-     * @param null|mixed $default
205
-     * @return false|int
206
-     */
207
-    public function getMatch($pattern, $default = null)
208
-    {
209
-        return $this->requestParameterDrillDown($pattern, $default, 'match');
210
-    }
211
-
212
-
213
-    /**
214
-     * check if a request parameter exists whose key matches the supplied wildcard pattern
215
-     * wildcards can be either of the following:
216
-     *      ? to represent a single character of any type
217
-     *      * to represent one or more characters of any type
218
-     * returns true if a match is found or false if not
219
-     *
220
-     * @param string $pattern
221
-     * @return false|int
222
-     */
223
-    public function matches($pattern)
224
-    {
225
-        return $this->requestParameterDrillDown($pattern, null, 'match') !== null;
226
-    }
227
-
228
-
229
-    /**
230
-     * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard
231
-     * @param string $pattern               A string including wildcards to be converted to a regex pattern
232
-     *                                      and used to search through the current request's parameter keys
233
-     * @param array  $request_params        The array of request parameters to search through
234
-     * @param mixed  $default               [optional] The value to be returned if no match is found.
235
-     *                                      Default is null
236
-     * @param string $return                [optional] Controls what kind of value is returned.
237
-     *                                      Options are:
238
-     *                                      'bool' will return true or false if match is found or not
239
-     *                                      'key' will return the first key found that matches the supplied pattern
240
-     *                                      'value' will return the value for the first request parameter
241
-     *                                      whose key matches the supplied pattern
242
-     *                                      Default is 'value'
243
-     * @return boolean|string
244
-     */
245
-    private function match($pattern, array $request_params, $default = null, $return = 'value')
246
-    {
247
-        $return = in_array($return, array('bool', 'key', 'value'), true)
248
-            ? $return
249
-            : 'is_set';
250
-        // replace wildcard chars with regex chars
251
-        $pattern = str_replace(
252
-            array("\*", "\?"),
253
-            array('.*', '.'),
254
-            preg_quote($pattern, '/')
255
-        );
256
-        foreach ($request_params as $key => $request_param) {
257
-            if (preg_match('/^' . $pattern . '$/is', $key)) {
258
-                // return value for request param
259
-                if ($return === 'value') {
260
-                    return $request_params[$key];
261
-                }
262
-                // or actual key or true just to indicate it was found
263
-                return $return === 'key' ? $key : true;
264
-            }
265
-        }
266
-        // match not found so return default value or false
267
-        return $return === 'value' ? $default : false;
268
-    }
269
-
270
-
271
-    /**
272
-     * the supplied key can be a simple string to represent a "top-level" request parameter
273
-     * or represent a key for a request parameter that is nested deeper within the request parameter array,
274
-     * by using square brackets to surround keys for deeper array elements.
275
-     * For example :
276
-     * if the supplied $key was: "first[second][third]"
277
-     * then this will attempt to drill down into the request parameter array to find a value.
278
-     * Given the following request parameters:
279
-     *  array(
280
-     *      'first' => array(
281
-     *          'second' => array(
282
-     *              'third' => 'has a value'
283
-     *          )
284
-     *      )
285
-     *  )
286
-     * would return true if default parameters were set
287
-     *
288
-     * @param string $callback
289
-     * @param        $key
290
-     * @param null   $default
291
-     * @param array  $request_params
292
-     * @return bool|mixed|null
293
-     */
294
-    private function requestParameterDrillDown(
295
-        $key,
296
-        $default = null,
297
-        $callback = 'is_set',
298
-        array $request_params = array()
299
-    ) {
300
-        $callback = in_array($callback, array('is_set', 'get', 'match'), true)
301
-            ? $callback
302
-            : 'is_set';
303
-        $request_params = ! empty($request_params)
304
-            ? $request_params
305
-            : $this->request;
306
-        // does incoming key represent an array like 'first[second][third]'  ?
307
-        if (strpos($key, '[') !== false) {
308
-            // turn it into an actual array
309
-            $key = str_replace(']', '', $key);
310
-            $keys = explode('[', $key);
311
-            $key = array_shift($keys);
312
-            if ($callback === 'match') {
313
-                $real_key = $this->match($key, $request_params, $default, 'key');
314
-                $key = $real_key ? $real_key : $key;
315
-            }
316
-            // check if top level key exists
317
-            if (isset($request_params[$key])) {
318
-                // build a new key to pass along like: 'second[third]'
319
-                // or just 'second' depending on depth of keys
320
-                $key_string = array_shift($keys);
321
-                if (! empty($keys)) {
322
-                    $key_string .= '[' . implode('][', $keys) . ']';
323
-                }
324
-                return $this->requestParameterDrillDown(
325
-                    $key_string,
326
-                    $default,
327
-                    $callback,
328
-                    $request_params[$key]
329
-                );
330
-            }
331
-        }
332
-        if ($callback === 'is_set') {
333
-            return isset($request_params[$key]);
334
-        }
335
-        if ($callback === 'match') {
336
-            return $this->match($key, $request_params, $default);
337
-        }
338
-        return isset($request_params[$key])
339
-            ? $request_params[$key]
340
-            : $default;
341
-    }
342
-
343
-
344
-    /**
345
-     * remove param
346
-     *
347
-     * @param      $key
348
-     * @param bool $unset_from_global_too
349
-     */
350
-    public function unSetRequestParam($key, $unset_from_global_too = false)
351
-    {
352
-        unset($this->request[$key]);
353
-        if ($unset_from_global_too) {
354
-            unset($_REQUEST[$key]);
355
-        }
356
-    }
357
-
358
-
359
-    /**
360
-     * @return string
361
-     */
362
-    public function ipAddress()
363
-    {
364
-        return $this->ip_address;
365
-    }
366
-
367
-
368
-    /**
369
-     * attempt to get IP address of current visitor from server
370
-     * plz see: http://stackoverflow.com/a/2031935/1475279
371
-     *
372
-     * @access public
373
-     * @return string
374
-     */
375
-    private function visitorIp()
376
-    {
377
-        $visitor_ip = '0.0.0.0';
378
-        $server_keys = array(
379
-            'HTTP_CLIENT_IP',
380
-            'HTTP_X_FORWARDED_FOR',
381
-            'HTTP_X_FORWARDED',
382
-            'HTTP_X_CLUSTER_CLIENT_IP',
383
-            'HTTP_FORWARDED_FOR',
384
-            'HTTP_FORWARDED',
385
-            'REMOTE_ADDR',
386
-        );
387
-        foreach ($server_keys as $key) {
388
-            if (isset($this->server[$key])) {
389
-                foreach (array_map('trim', explode(',', $this->server[$key])) as $ip) {
390
-                    if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) {
391
-                        $visitor_ip = $ip;
392
-                    }
393
-                }
394
-            }
395
-        }
396
-        return $visitor_ip;
397
-    }
398
-
399
-
400
-    /**
401
-     * @return string
402
-     */
403
-    public function requestUri()
404
-    {
405
-        $request_uri = filter_input(
406
-            INPUT_SERVER,
407
-            'REQUEST_URI',
408
-            FILTER_SANITIZE_URL,
409
-            FILTER_NULL_ON_FAILURE
410
-        );
411
-        if (empty($request_uri)) {
412
-            // fallback sanitization if the above fails
413
-            $request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']);
414
-        }
415
-        return $request_uri;
416
-    }
417
-
418
-
419
-    /**
420
-     * @return string
421
-     */
422
-    public function userAgent()
423
-    {
424
-        return $this->user_agent;
425
-    }
426
-
427
-
428
-    /**
429
-     * @param string $user_agent
430
-     */
431
-    public function setUserAgent($user_agent = '')
432
-    {
433
-        if ($user_agent === '' || ! is_string($user_agent)) {
434
-            $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string)esc_attr($_SERVER['HTTP_USER_AGENT']) : '';
435
-        }
436
-        $this->user_agent = $user_agent;
437
-    }
438
-
439
-
440
-    /**
441
-     * @return bool
442
-     */
443
-    public function isBot()
444
-    {
445
-        return $this->is_bot;
446
-    }
447
-
448
-
449
-    /**
450
-     * @param bool $is_bot
451
-     */
452
-    public function setIsBot($is_bot)
453
-    {
454
-        $this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN);
455
-    }
456
-
457
-
458
-    /**
459
-     * @return bool
460
-     */
461
-    public function isActivation()
462
-    {
463
-        return $this->request_type->isActivation();
464
-    }
465
-
466
-
467
-    /**
468
-     * @param $is_activation
469
-     * @return bool
470
-     */
471
-    public function setIsActivation($is_activation)
472
-    {
473
-        return $this->request_type->setIsActivation($is_activation);
474
-    }
475
-
476
-
477
-    /**
478
-     * @return bool
479
-     */
480
-    public function isAdmin()
481
-    {
482
-        return $this->request_type->isAdmin();
483
-    }
484
-
485
-
486
-    /**
487
-     * @return bool
488
-     */
489
-    public function isAdminAjax()
490
-    {
491
-        return $this->request_type->isAdminAjax();
492
-    }
493
-
494
-
495
-    /**
496
-     * @return bool
497
-     */
498
-    public function isAjax()
499
-    {
500
-        return $this->request_type->isAjax();
501
-    }
502
-
503
-
504
-    /**
505
-     * @return bool
506
-     */
507
-    public function isEeAjax()
508
-    {
509
-        return $this->request_type->isEeAjax();
510
-    }
511
-
512
-
513
-    /**
514
-     * @return bool
515
-     */
516
-    public function isOtherAjax()
517
-    {
518
-        return $this->request_type->isOtherAjax();
519
-    }
520
-
521
-
522
-    /**
523
-     * @return bool
524
-     */
525
-    public function isApi()
526
-    {
527
-        return $this->request_type->isApi();
528
-    }
529
-
530
-
531
-    /**
532
-     * @return bool
533
-     */
534
-    public function isCli()
535
-    {
536
-        return $this->request_type->isCli();
537
-    }
538
-
539
-
540
-    /**
541
-     * @return bool
542
-     */
543
-    public function isCron()
544
-    {
545
-        return $this->request_type->isCron();
546
-    }
547
-
548
-
549
-    /**
550
-     * @return bool
551
-     */
552
-    public function isFeed()
553
-    {
554
-        return $this->request_type->isFeed();
555
-    }
556
-
557
-
558
-    /**
559
-     * @return bool
560
-     */
561
-    public function isFrontend()
562
-    {
563
-        return $this->request_type->isFrontend();
564
-    }
565
-
566
-
567
-    /**
568
-     * @return bool
569
-     */
570
-    public function isFrontAjax()
571
-    {
572
-        return $this->request_type->isFrontAjax();
573
-    }
574
-
575
-
576
-    /**
577
-     * @return bool
578
-     */
579
-    public function isIframe()
580
-    {
581
-        return $this->request_type->isIframe();
582
-    }
583
-
584
-
585
-    /**
586
-     * @return string
587
-     */
588
-    public function slug()
589
-    {
590
-        return $this->request_type->slug();
591
-    }
19
+	/**
20
+	 * $_GET parameters
21
+	 *
22
+	 * @var array $get
23
+	 */
24
+	private $get;
25
+
26
+	/**
27
+	 * $_POST parameters
28
+	 *
29
+	 * @var array $post
30
+	 */
31
+	private $post;
32
+
33
+	/**
34
+	 * $_COOKIE parameters
35
+	 *
36
+	 * @var array $cookie
37
+	 */
38
+	private $cookie;
39
+
40
+	/**
41
+	 * $_SERVER parameters
42
+	 *
43
+	 * @var array $server
44
+	 */
45
+	private $server;
46
+
47
+	/**
48
+	 * $_REQUEST parameters
49
+	 *
50
+	 * @var array $request
51
+	 */
52
+	private $request;
53
+
54
+	/**
55
+	 * @var RequestTypeContextCheckerInterface
56
+	 */
57
+	private $request_type;
58
+
59
+	/**
60
+	 * IP address for request
61
+	 *
62
+	 * @var string $ip_address
63
+	 */
64
+	private $ip_address;
65
+
66
+	/**
67
+	 * @var string $user_agent
68
+	 */
69
+	private $user_agent;
70
+
71
+	/**
72
+	 * true if current user appears to be some kind of bot
73
+	 *
74
+	 * @var bool $is_bot
75
+	 */
76
+	private $is_bot;
77
+
78
+
79
+	/**
80
+	 * @param array $get
81
+	 * @param array $post
82
+	 * @param array $cookie
83
+	 * @param array $server
84
+	 */
85
+	public function __construct(array $get, array $post, array $cookie, array $server)
86
+	{
87
+		// grab request vars
88
+		$this->get = $get;
89
+		$this->post = $post;
90
+		$this->cookie = $cookie;
91
+		$this->server = $server;
92
+		$this->request = array_merge($this->get, $this->post);
93
+		$this->ip_address = $this->visitorIp();
94
+	}
95
+
96
+
97
+	/**
98
+	 * @param RequestTypeContextCheckerInterface $type
99
+	 */
100
+	public function setRequestTypeContextChecker(RequestTypeContextCheckerInterface $type)
101
+	{
102
+		$this->request_type = $type;
103
+	}
104
+
105
+
106
+	/**
107
+	 * @return array
108
+	 */
109
+	public function getParams()
110
+	{
111
+		return $this->get;
112
+	}
113
+
114
+
115
+	/**
116
+	 * @return array
117
+	 */
118
+	public function postParams()
119
+	{
120
+		return $this->post;
121
+	}
122
+
123
+
124
+	/**
125
+	 * @return array
126
+	 */
127
+	public function cookieParams()
128
+	{
129
+		return $this->cookie;
130
+	}
131
+
132
+
133
+	/**
134
+	 * @return array
135
+	 */
136
+	public function serverParams()
137
+	{
138
+		return $this->server;
139
+	}
140
+
141
+
142
+	/**
143
+	 * returns contents of $_REQUEST
144
+	 *
145
+	 * @return array
146
+	 */
147
+	public function requestParams()
148
+	{
149
+		return $this->request;
150
+	}
151
+
152
+
153
+	/**
154
+	 * @param      $key
155
+	 * @param      $value
156
+	 * @param bool $override_ee
157
+	 * @return    void
158
+	 */
159
+	public function setRequestParam($key, $value, $override_ee = false)
160
+	{
161
+		// don't allow "ee" to be overwritten unless explicitly instructed to do so
162
+		if ($key !== 'ee'
163
+			|| ($key === 'ee' && empty($this->request['ee']))
164
+			|| ($key === 'ee' && ! empty($this->request['ee']) && $override_ee)
165
+		) {
166
+			$this->request[$key] = $value;
167
+		}
168
+	}
169
+
170
+
171
+	/**
172
+	 * returns   the value for a request param if the given key exists
173
+	 *
174
+	 * @param       $key
175
+	 * @param null  $default
176
+	 * @return mixed
177
+	 */
178
+	public function getRequestParam($key, $default = null)
179
+	{
180
+		return $this->requestParameterDrillDown($key, $default, 'get');
181
+	}
182
+
183
+
184
+	/**
185
+	 * check if param exists
186
+	 *
187
+	 * @param       $key
188
+	 * @return bool
189
+	 */
190
+	public function requestParamIsSet($key)
191
+	{
192
+		return $this->requestParameterDrillDown($key);
193
+	}
194
+
195
+
196
+	/**
197
+	 * check if a request parameter exists whose key that matches the supplied wildcard pattern
198
+	 * and return the value for the first match found
199
+	 * wildcards can be either of the following:
200
+	 *      ? to represent a single character of any type
201
+	 *      * to represent one or more characters of any type
202
+	 *
203
+	 * @param string     $pattern
204
+	 * @param null|mixed $default
205
+	 * @return false|int
206
+	 */
207
+	public function getMatch($pattern, $default = null)
208
+	{
209
+		return $this->requestParameterDrillDown($pattern, $default, 'match');
210
+	}
211
+
212
+
213
+	/**
214
+	 * check if a request parameter exists whose key matches the supplied wildcard pattern
215
+	 * wildcards can be either of the following:
216
+	 *      ? to represent a single character of any type
217
+	 *      * to represent one or more characters of any type
218
+	 * returns true if a match is found or false if not
219
+	 *
220
+	 * @param string $pattern
221
+	 * @return false|int
222
+	 */
223
+	public function matches($pattern)
224
+	{
225
+		return $this->requestParameterDrillDown($pattern, null, 'match') !== null;
226
+	}
227
+
228
+
229
+	/**
230
+	 * @see https://stackoverflow.com/questions/6163055/php-string-matching-with-wildcard
231
+	 * @param string $pattern               A string including wildcards to be converted to a regex pattern
232
+	 *                                      and used to search through the current request's parameter keys
233
+	 * @param array  $request_params        The array of request parameters to search through
234
+	 * @param mixed  $default               [optional] The value to be returned if no match is found.
235
+	 *                                      Default is null
236
+	 * @param string $return                [optional] Controls what kind of value is returned.
237
+	 *                                      Options are:
238
+	 *                                      'bool' will return true or false if match is found or not
239
+	 *                                      'key' will return the first key found that matches the supplied pattern
240
+	 *                                      'value' will return the value for the first request parameter
241
+	 *                                      whose key matches the supplied pattern
242
+	 *                                      Default is 'value'
243
+	 * @return boolean|string
244
+	 */
245
+	private function match($pattern, array $request_params, $default = null, $return = 'value')
246
+	{
247
+		$return = in_array($return, array('bool', 'key', 'value'), true)
248
+			? $return
249
+			: 'is_set';
250
+		// replace wildcard chars with regex chars
251
+		$pattern = str_replace(
252
+			array("\*", "\?"),
253
+			array('.*', '.'),
254
+			preg_quote($pattern, '/')
255
+		);
256
+		foreach ($request_params as $key => $request_param) {
257
+			if (preg_match('/^' . $pattern . '$/is', $key)) {
258
+				// return value for request param
259
+				if ($return === 'value') {
260
+					return $request_params[$key];
261
+				}
262
+				// or actual key or true just to indicate it was found
263
+				return $return === 'key' ? $key : true;
264
+			}
265
+		}
266
+		// match not found so return default value or false
267
+		return $return === 'value' ? $default : false;
268
+	}
269
+
270
+
271
+	/**
272
+	 * the supplied key can be a simple string to represent a "top-level" request parameter
273
+	 * or represent a key for a request parameter that is nested deeper within the request parameter array,
274
+	 * by using square brackets to surround keys for deeper array elements.
275
+	 * For example :
276
+	 * if the supplied $key was: "first[second][third]"
277
+	 * then this will attempt to drill down into the request parameter array to find a value.
278
+	 * Given the following request parameters:
279
+	 *  array(
280
+	 *      'first' => array(
281
+	 *          'second' => array(
282
+	 *              'third' => 'has a value'
283
+	 *          )
284
+	 *      )
285
+	 *  )
286
+	 * would return true if default parameters were set
287
+	 *
288
+	 * @param string $callback
289
+	 * @param        $key
290
+	 * @param null   $default
291
+	 * @param array  $request_params
292
+	 * @return bool|mixed|null
293
+	 */
294
+	private function requestParameterDrillDown(
295
+		$key,
296
+		$default = null,
297
+		$callback = 'is_set',
298
+		array $request_params = array()
299
+	) {
300
+		$callback = in_array($callback, array('is_set', 'get', 'match'), true)
301
+			? $callback
302
+			: 'is_set';
303
+		$request_params = ! empty($request_params)
304
+			? $request_params
305
+			: $this->request;
306
+		// does incoming key represent an array like 'first[second][third]'  ?
307
+		if (strpos($key, '[') !== false) {
308
+			// turn it into an actual array
309
+			$key = str_replace(']', '', $key);
310
+			$keys = explode('[', $key);
311
+			$key = array_shift($keys);
312
+			if ($callback === 'match') {
313
+				$real_key = $this->match($key, $request_params, $default, 'key');
314
+				$key = $real_key ? $real_key : $key;
315
+			}
316
+			// check if top level key exists
317
+			if (isset($request_params[$key])) {
318
+				// build a new key to pass along like: 'second[third]'
319
+				// or just 'second' depending on depth of keys
320
+				$key_string = array_shift($keys);
321
+				if (! empty($keys)) {
322
+					$key_string .= '[' . implode('][', $keys) . ']';
323
+				}
324
+				return $this->requestParameterDrillDown(
325
+					$key_string,
326
+					$default,
327
+					$callback,
328
+					$request_params[$key]
329
+				);
330
+			}
331
+		}
332
+		if ($callback === 'is_set') {
333
+			return isset($request_params[$key]);
334
+		}
335
+		if ($callback === 'match') {
336
+			return $this->match($key, $request_params, $default);
337
+		}
338
+		return isset($request_params[$key])
339
+			? $request_params[$key]
340
+			: $default;
341
+	}
342
+
343
+
344
+	/**
345
+	 * remove param
346
+	 *
347
+	 * @param      $key
348
+	 * @param bool $unset_from_global_too
349
+	 */
350
+	public function unSetRequestParam($key, $unset_from_global_too = false)
351
+	{
352
+		unset($this->request[$key]);
353
+		if ($unset_from_global_too) {
354
+			unset($_REQUEST[$key]);
355
+		}
356
+	}
357
+
358
+
359
+	/**
360
+	 * @return string
361
+	 */
362
+	public function ipAddress()
363
+	{
364
+		return $this->ip_address;
365
+	}
366
+
367
+
368
+	/**
369
+	 * attempt to get IP address of current visitor from server
370
+	 * plz see: http://stackoverflow.com/a/2031935/1475279
371
+	 *
372
+	 * @access public
373
+	 * @return string
374
+	 */
375
+	private function visitorIp()
376
+	{
377
+		$visitor_ip = '0.0.0.0';
378
+		$server_keys = array(
379
+			'HTTP_CLIENT_IP',
380
+			'HTTP_X_FORWARDED_FOR',
381
+			'HTTP_X_FORWARDED',
382
+			'HTTP_X_CLUSTER_CLIENT_IP',
383
+			'HTTP_FORWARDED_FOR',
384
+			'HTTP_FORWARDED',
385
+			'REMOTE_ADDR',
386
+		);
387
+		foreach ($server_keys as $key) {
388
+			if (isset($this->server[$key])) {
389
+				foreach (array_map('trim', explode(',', $this->server[$key])) as $ip) {
390
+					if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) {
391
+						$visitor_ip = $ip;
392
+					}
393
+				}
394
+			}
395
+		}
396
+		return $visitor_ip;
397
+	}
398
+
399
+
400
+	/**
401
+	 * @return string
402
+	 */
403
+	public function requestUri()
404
+	{
405
+		$request_uri = filter_input(
406
+			INPUT_SERVER,
407
+			'REQUEST_URI',
408
+			FILTER_SANITIZE_URL,
409
+			FILTER_NULL_ON_FAILURE
410
+		);
411
+		if (empty($request_uri)) {
412
+			// fallback sanitization if the above fails
413
+			$request_uri = wp_sanitize_redirect($this->server['REQUEST_URI']);
414
+		}
415
+		return $request_uri;
416
+	}
417
+
418
+
419
+	/**
420
+	 * @return string
421
+	 */
422
+	public function userAgent()
423
+	{
424
+		return $this->user_agent;
425
+	}
426
+
427
+
428
+	/**
429
+	 * @param string $user_agent
430
+	 */
431
+	public function setUserAgent($user_agent = '')
432
+	{
433
+		if ($user_agent === '' || ! is_string($user_agent)) {
434
+			$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string)esc_attr($_SERVER['HTTP_USER_AGENT']) : '';
435
+		}
436
+		$this->user_agent = $user_agent;
437
+	}
438
+
439
+
440
+	/**
441
+	 * @return bool
442
+	 */
443
+	public function isBot()
444
+	{
445
+		return $this->is_bot;
446
+	}
447
+
448
+
449
+	/**
450
+	 * @param bool $is_bot
451
+	 */
452
+	public function setIsBot($is_bot)
453
+	{
454
+		$this->is_bot = filter_var($is_bot, FILTER_VALIDATE_BOOLEAN);
455
+	}
456
+
457
+
458
+	/**
459
+	 * @return bool
460
+	 */
461
+	public function isActivation()
462
+	{
463
+		return $this->request_type->isActivation();
464
+	}
465
+
466
+
467
+	/**
468
+	 * @param $is_activation
469
+	 * @return bool
470
+	 */
471
+	public function setIsActivation($is_activation)
472
+	{
473
+		return $this->request_type->setIsActivation($is_activation);
474
+	}
475
+
476
+
477
+	/**
478
+	 * @return bool
479
+	 */
480
+	public function isAdmin()
481
+	{
482
+		return $this->request_type->isAdmin();
483
+	}
484
+
485
+
486
+	/**
487
+	 * @return bool
488
+	 */
489
+	public function isAdminAjax()
490
+	{
491
+		return $this->request_type->isAdminAjax();
492
+	}
493
+
494
+
495
+	/**
496
+	 * @return bool
497
+	 */
498
+	public function isAjax()
499
+	{
500
+		return $this->request_type->isAjax();
501
+	}
502
+
503
+
504
+	/**
505
+	 * @return bool
506
+	 */
507
+	public function isEeAjax()
508
+	{
509
+		return $this->request_type->isEeAjax();
510
+	}
511
+
512
+
513
+	/**
514
+	 * @return bool
515
+	 */
516
+	public function isOtherAjax()
517
+	{
518
+		return $this->request_type->isOtherAjax();
519
+	}
520
+
521
+
522
+	/**
523
+	 * @return bool
524
+	 */
525
+	public function isApi()
526
+	{
527
+		return $this->request_type->isApi();
528
+	}
529
+
530
+
531
+	/**
532
+	 * @return bool
533
+	 */
534
+	public function isCli()
535
+	{
536
+		return $this->request_type->isCli();
537
+	}
538
+
539
+
540
+	/**
541
+	 * @return bool
542
+	 */
543
+	public function isCron()
544
+	{
545
+		return $this->request_type->isCron();
546
+	}
547
+
548
+
549
+	/**
550
+	 * @return bool
551
+	 */
552
+	public function isFeed()
553
+	{
554
+		return $this->request_type->isFeed();
555
+	}
556
+
557
+
558
+	/**
559
+	 * @return bool
560
+	 */
561
+	public function isFrontend()
562
+	{
563
+		return $this->request_type->isFrontend();
564
+	}
565
+
566
+
567
+	/**
568
+	 * @return bool
569
+	 */
570
+	public function isFrontAjax()
571
+	{
572
+		return $this->request_type->isFrontAjax();
573
+	}
574
+
575
+
576
+	/**
577
+	 * @return bool
578
+	 */
579
+	public function isIframe()
580
+	{
581
+		return $this->request_type->isIframe();
582
+	}
583
+
584
+
585
+	/**
586
+	 * @return string
587
+	 */
588
+	public function slug()
589
+	{
590
+		return $this->request_type->slug();
591
+	}
592 592
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
             preg_quote($pattern, '/')
255 255
         );
256 256
         foreach ($request_params as $key => $request_param) {
257
-            if (preg_match('/^' . $pattern . '$/is', $key)) {
257
+            if (preg_match('/^'.$pattern.'$/is', $key)) {
258 258
                 // return value for request param
259 259
                 if ($return === 'value') {
260 260
                     return $request_params[$key];
@@ -318,8 +318,8 @@  discard block
 block discarded – undo
318 318
                 // build a new key to pass along like: 'second[third]'
319 319
                 // or just 'second' depending on depth of keys
320 320
                 $key_string = array_shift($keys);
321
-                if (! empty($keys)) {
322
-                    $key_string .= '[' . implode('][', $keys) . ']';
321
+                if ( ! empty($keys)) {
322
+                    $key_string .= '['.implode('][', $keys).']';
323 323
                 }
324 324
                 return $this->requestParameterDrillDown(
325 325
                     $key_string,
@@ -431,7 +431,7 @@  discard block
 block discarded – undo
431 431
     public function setUserAgent($user_agent = '')
432 432
     {
433 433
         if ($user_agent === '' || ! is_string($user_agent)) {
434
-            $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string)esc_attr($_SERVER['HTTP_USER_AGENT']) : '';
434
+            $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? (string) esc_attr($_SERVER['HTTP_USER_AGENT']) : '';
435 435
         }
436 436
         $this->user_agent = $user_agent;
437 437
     }
Please login to merge, or discard this patch.
core/services/collections/LooseCollection.php 2 patches
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -16,37 +16,37 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * setCollectionInterface
21
-     *
22
-     * @access protected
23
-     * @param  string $collection_interface
24
-     */
25
-    protected function setCollectionInterface($collection_interface)
26
-    {
27
-        $this->collection_interface = '';
28
-    }
19
+	/**
20
+	 * setCollectionInterface
21
+	 *
22
+	 * @access protected
23
+	 * @param  string $collection_interface
24
+	 */
25
+	protected function setCollectionInterface($collection_interface)
26
+	{
27
+		$this->collection_interface = '';
28
+	}
29 29
 
30 30
 
31
-    /**
32
-     * add
33
-     * attaches an object to the Collection
34
-     * and sets any supplied data associated with the current iterator entry
35
-     * by calling EE_Object_Collection::set_identifier()
36
-     *
37
-     * @access public
38
-     * @param  mixed $object
39
-     * @param  mixed $identifier
40
-     * @return bool
41
-     * @throws InvalidEntityException
42
-     */
43
-    public function add($object, $identifier = null)
44
-    {
45
-        if (! is_object($object)) {
46
-            throw new InvalidEntityException($object, 'object');
47
-        }
48
-        $this->attach($object);
49
-        $this->setIdentifier($object, $identifier);
50
-        return $this->contains($object);
51
-    }
31
+	/**
32
+	 * add
33
+	 * attaches an object to the Collection
34
+	 * and sets any supplied data associated with the current iterator entry
35
+	 * by calling EE_Object_Collection::set_identifier()
36
+	 *
37
+	 * @access public
38
+	 * @param  mixed $object
39
+	 * @param  mixed $identifier
40
+	 * @return bool
41
+	 * @throws InvalidEntityException
42
+	 */
43
+	public function add($object, $identifier = null)
44
+	{
45
+		if (! is_object($object)) {
46
+			throw new InvalidEntityException($object, 'object');
47
+		}
48
+		$this->attach($object);
49
+		$this->setIdentifier($object, $identifier);
50
+		return $this->contains($object);
51
+	}
52 52
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@
 block discarded – undo
42 42
      */
43 43
     public function add($object, $identifier = null)
44 44
     {
45
-        if (! is_object($object)) {
45
+        if ( ! is_object($object)) {
46 46
             throw new InvalidEntityException($object, 'object');
47 47
         }
48 48
         $this->attach($object);
Please login to merge, or discard this patch.
core/services/collections/CollectionDetails.php 2 patches
Indentation   +347 added lines, -347 removed lines patch added patch discarded remove patch
@@ -42,351 +42,351 @@
 block discarded – undo
42 42
 class CollectionDetails implements CollectionDetailsInterface
43 43
 {
44 44
 
45
-    /**
46
-     * if $identifier_type is set to this,
47
-     * then the collection will use each object's spl_object_hash() as it's identifier
48
-     */
49
-    const ID_OBJECT_HASH = 'identifier-uses-spl-object-hash';
50
-
51
-    /**
52
-     * if $identifier_type is set to this,
53
-     * then the collection will use each object's class name as it's identifier
54
-     */
55
-    const ID_CLASS_NAME = 'identifier-uses-object-class-name';
56
-
57
-    /**
58
-     * if $identifier_type is set to this,
59
-     * then the collection will use the return value from a specified callback method on each object
60
-     */
61
-    const ID_CALLBACK_METHOD = 'identifier-uses-callback-method';
62
-
63
-    /**
64
-     * The interface used for controlling what gets added to the collection
65
-     *
66
-     * @var string $collection_interface
67
-     */
68
-    protected $collection_interface = '';
69
-
70
-    /**
71
-     * a unique name used to identify the collection in filter names
72
-     * supplied value is run through sanitize_title_with_dashes(),
73
-     * but then also converts dashes to underscores
74
-     *
75
-     * @var string $collection_name
76
-     */
77
-    protected $collection_name = '';
78
-
79
-    /**
80
-     * what the collection uses for the object identifier.
81
-     * corresponds to one of the class constants above.
82
-     * CollectionDetails::ID_OBJECT_HASH will use spl_object_hash( object ) for the identifier
83
-     * CollectionDetails::ID_CLASS_NAME will use get_class( object ) for the identifier
84
-     * CollectionDetails::ID_CALLBACK_METHOD will use a callback for the identifier
85
-     * defaults to using spl_object_hash() so that multiple objects of the same class can be added
86
-     *
87
-     * @var string $identifier_type
88
-     */
89
-    protected $identifier_type = CollectionDetails::ID_OBJECT_HASH;
90
-
91
-    /**
92
-     * the pattern applied to paths when searching for class files to add to the collection
93
-     * ie: "My_Awesome_*.class.php"
94
-     * defaults to "*.php"
95
-     *
96
-     * @var string $file_mask
97
-     */
98
-    protected $file_mask = '';
99
-
100
-    /**
101
-     * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD,
102
-     * then this specifies the method to use on each entity.
103
-     * If the callback method does not exist, then an exception will be thrown
104
-     *
105
-     * @var string $identifier_callback
106
-     */
107
-    protected $identifier_callback = '';
108
-
109
-    /**
110
-     * an array of Fully Qualified Class Names
111
-     *  for example:
112
-     *  $FQCNs = array(
113
-     *      '/Fully/Qualified/ClassNameA'
114
-     *      '/Fully/Qualified/Other/ClassNameB'
115
-     *  );
116
-     *
117
-     * @var array $collection_FQCNs
118
-     */
119
-    protected $collection_FQCNs = array();
120
-
121
-    /**
122
-     * an array of full server paths to folders containing files to be loaded into collection
123
-     *  for example:
124
-     *  $paths = array(
125
-     *      '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA
126
-     *      '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB
127
-     *  );
128
-     *
129
-     * @var array $collection_paths
130
-     */
131
-    protected $collection_paths = array();
132
-
133
-    /**
134
-     * @var LocatorInterface $file_locator
135
-     */
136
-    protected $file_locator;
137
-
138
-
139
-    /**
140
-     * CollectionDetails constructor.
141
-     *
142
-     * @access public
143
-     * @param string           $collection_name
144
-     * @param string           $collection_interface
145
-     * @param array            $collection_FQCNs
146
-     * @param array            $collection_paths
147
-     * @param string           $file_mask
148
-     * @param string           $identifier_type
149
-     * @param string           $identifier_callback
150
-     * @param LocatorInterface $file_locator
151
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
152
-     * @throws \EventEspresso\core\exceptions\InvalidFilePathException
153
-     * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
154
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
155
-     * @throws \EventEspresso\core\exceptions\InvalidClassException
156
-     */
157
-    public function __construct(
158
-        $collection_name,
159
-        $collection_interface,
160
-        $collection_FQCNs = array(),
161
-        $collection_paths = array(),
162
-        $file_mask = '',
163
-        $identifier_type = CollectionDetails::ID_OBJECT_HASH,
164
-        $identifier_callback = '',
165
-        LocatorInterface $file_locator = null
166
-    ) {
167
-        $this->setCollectionName($collection_name);
168
-        $this->setCollectionInterface($collection_interface);
169
-        $this->setCollectionFQCNs($collection_FQCNs);
170
-        $this->setCollectionPaths($collection_paths);
171
-        $this->setFileMasks($file_mask);
172
-        $this->setIdentifierType($identifier_type);
173
-        $this->setIdentifierCallback($identifier_callback);
174
-        $this->file_locator = $file_locator;
175
-    }
176
-
177
-
178
-    /**
179
-     * @access public
180
-     * @return mixed
181
-     */
182
-    public function getCollectionInterface()
183
-    {
184
-        return $this->collection_interface;
185
-    }
186
-
187
-
188
-    /**
189
-     * @access protected
190
-     * @param string $collection_interface
191
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
192
-     */
193
-    protected function setCollectionInterface($collection_interface)
194
-    {
195
-        if (! (interface_exists($collection_interface) || class_exists($collection_interface))) {
196
-            throw new InvalidInterfaceException($collection_interface);
197
-        }
198
-        $this->collection_interface = $collection_interface;
199
-    }
200
-
201
-
202
-    /**
203
-     * the collection name will be used for creating dynamic filters
204
-     *
205
-     * @access public
206
-     * @return string
207
-     */
208
-    public function collectionName()
209
-    {
210
-        return $this->collection_name;
211
-    }
212
-
213
-
214
-    /**
215
-     * sanitizes collection name and converts spaces and dashes to underscores
216
-     *
217
-     * @access protected
218
-     * @param string $collection_name
219
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
220
-     */
221
-    protected function setCollectionName($collection_name)
222
-    {
223
-        if (! is_string($collection_name)) {
224
-            throw new InvalidDataTypeException('$collection_name', $collection_name, 'string');
225
-        }
226
-        $this->collection_name = str_replace(
227
-            '-',
228
-            '_',
229
-            sanitize_title_with_dashes($collection_name, '', 'save')
230
-        );
231
-    }
232
-
233
-
234
-    /**
235
-     * @access public
236
-     * @return string
237
-     */
238
-    public function identifierType()
239
-    {
240
-        return $this->identifier_type;
241
-    }
242
-
243
-
244
-    /**
245
-     * @access protected
246
-     * @param string $identifier_type
247
-     * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
248
-     */
249
-    protected function setIdentifierType($identifier_type)
250
-    {
251
-        if (! ($identifier_type === CollectionDetails::ID_CLASS_NAME
252
-            || $identifier_type === CollectionDetails::ID_OBJECT_HASH
253
-            || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD
254
-        )) {
255
-            throw new InvalidIdentifierException(
256
-                $identifier_type,
257
-                'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD'
258
-            );
259
-        }
260
-        $this->identifier_type = $identifier_type;
261
-    }
262
-
263
-
264
-    /**
265
-     * @access public
266
-     * @return string
267
-     */
268
-    public function identifierCallback()
269
-    {
270
-        return $this->identifier_callback;
271
-    }
272
-
273
-
274
-    /**
275
-     * @access protected
276
-     * @param string $identifier_callback
277
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
278
-     */
279
-    protected function setIdentifierCallback($identifier_callback = 'identifier')
280
-    {
281
-        if (! is_string($identifier_callback)) {
282
-            throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string');
283
-        }
284
-        $this->identifier_callback = $identifier_callback;
285
-    }
286
-
287
-
288
-    /**
289
-     * @access public
290
-     * @return string
291
-     */
292
-    public function getFileMask()
293
-    {
294
-        return $this->file_mask;
295
-    }
296
-
297
-
298
-    /**
299
-     * sets the file mask which is then used to filter what files get loaded
300
-     * when searching for classes to add to the collection. Defaults to '*.php'
301
-     *
302
-     * @access protected
303
-     * @param string $file_mask
304
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
305
-     */
306
-    protected function setFileMasks($file_mask)
307
-    {
308
-        $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php';
309
-        // we know our default is a string, so if it's not a string now,
310
-        // then that means the incoming parameter was something else
311
-        if (! is_string($this->file_mask)) {
312
-            throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string');
313
-        }
314
-    }
315
-
316
-
317
-    /**
318
-     * @access public
319
-     * @return string
320
-     */
321
-    public function getCollectionFQCNs()
322
-    {
323
-        return $this->collection_FQCNs;
324
-    }
325
-
326
-
327
-    /**
328
-     * @access public
329
-     * @param string|array $collection_FQCNs
330
-     * @throws \EventEspresso\core\exceptions\InvalidClassException
331
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
332
-     */
333
-    public function setCollectionFQCNs($collection_FQCNs)
334
-    {
335
-        foreach ((array)$collection_FQCNs as $collection_FQCN) {
336
-            if (! empty($collection_FQCN)) {
337
-                if (class_exists($collection_FQCN)) {
338
-                    $this->collection_FQCNs[] = $collection_FQCN;
339
-                } else {
340
-                    foreach ($this->getFQCNsFromPartialNamespace($collection_FQCN) as $FQCN) {
341
-                        $this->collection_FQCNs[] = $FQCN;
342
-                    }
343
-                }
344
-            }
345
-        }
346
-    }
347
-
348
-
349
-    /**
350
-     * @access protected
351
-     * @param  string $partial_FQCN
352
-     * @return array
353
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
354
-     * @throws \EventEspresso\core\exceptions\InvalidClassException
355
-     */
356
-    protected function getFQCNsFromPartialNamespace($partial_FQCN)
357
-    {
358
-        if (! $this->file_locator instanceof FqcnLocator) {
359
-            $this->file_locator = new FqcnLocator();
360
-        }
361
-        $this->file_locator->locate($partial_FQCN);
362
-        return $this->file_locator->getFQCNs();
363
-    }
364
-
365
-
366
-    /**
367
-     * @access public
368
-     * @return string
369
-     */
370
-    public function getCollectionPaths()
371
-    {
372
-        return $this->collection_paths;
373
-    }
374
-
375
-
376
-    /**
377
-     * @access public
378
-     * @param string|array $collection_paths
379
-     * @throws \EventEspresso\core\exceptions\InvalidFilePathException
380
-     */
381
-    public function setCollectionPaths($collection_paths)
382
-    {
383
-        foreach ((array)$collection_paths as $collection_path) {
384
-            if (! empty($collection_path)) {
385
-                if (! is_readable($collection_path)) {
386
-                    throw new InvalidFilePathException($collection_path);
387
-                }
388
-                $this->collection_paths[] = $collection_path;
389
-            }
390
-        }
391
-    }
45
+	/**
46
+	 * if $identifier_type is set to this,
47
+	 * then the collection will use each object's spl_object_hash() as it's identifier
48
+	 */
49
+	const ID_OBJECT_HASH = 'identifier-uses-spl-object-hash';
50
+
51
+	/**
52
+	 * if $identifier_type is set to this,
53
+	 * then the collection will use each object's class name as it's identifier
54
+	 */
55
+	const ID_CLASS_NAME = 'identifier-uses-object-class-name';
56
+
57
+	/**
58
+	 * if $identifier_type is set to this,
59
+	 * then the collection will use the return value from a specified callback method on each object
60
+	 */
61
+	const ID_CALLBACK_METHOD = 'identifier-uses-callback-method';
62
+
63
+	/**
64
+	 * The interface used for controlling what gets added to the collection
65
+	 *
66
+	 * @var string $collection_interface
67
+	 */
68
+	protected $collection_interface = '';
69
+
70
+	/**
71
+	 * a unique name used to identify the collection in filter names
72
+	 * supplied value is run through sanitize_title_with_dashes(),
73
+	 * but then also converts dashes to underscores
74
+	 *
75
+	 * @var string $collection_name
76
+	 */
77
+	protected $collection_name = '';
78
+
79
+	/**
80
+	 * what the collection uses for the object identifier.
81
+	 * corresponds to one of the class constants above.
82
+	 * CollectionDetails::ID_OBJECT_HASH will use spl_object_hash( object ) for the identifier
83
+	 * CollectionDetails::ID_CLASS_NAME will use get_class( object ) for the identifier
84
+	 * CollectionDetails::ID_CALLBACK_METHOD will use a callback for the identifier
85
+	 * defaults to using spl_object_hash() so that multiple objects of the same class can be added
86
+	 *
87
+	 * @var string $identifier_type
88
+	 */
89
+	protected $identifier_type = CollectionDetails::ID_OBJECT_HASH;
90
+
91
+	/**
92
+	 * the pattern applied to paths when searching for class files to add to the collection
93
+	 * ie: "My_Awesome_*.class.php"
94
+	 * defaults to "*.php"
95
+	 *
96
+	 * @var string $file_mask
97
+	 */
98
+	protected $file_mask = '';
99
+
100
+	/**
101
+	 * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD,
102
+	 * then this specifies the method to use on each entity.
103
+	 * If the callback method does not exist, then an exception will be thrown
104
+	 *
105
+	 * @var string $identifier_callback
106
+	 */
107
+	protected $identifier_callback = '';
108
+
109
+	/**
110
+	 * an array of Fully Qualified Class Names
111
+	 *  for example:
112
+	 *  $FQCNs = array(
113
+	 *      '/Fully/Qualified/ClassNameA'
114
+	 *      '/Fully/Qualified/Other/ClassNameB'
115
+	 *  );
116
+	 *
117
+	 * @var array $collection_FQCNs
118
+	 */
119
+	protected $collection_FQCNs = array();
120
+
121
+	/**
122
+	 * an array of full server paths to folders containing files to be loaded into collection
123
+	 *  for example:
124
+	 *  $paths = array(
125
+	 *      '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA
126
+	 *      '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB
127
+	 *  );
128
+	 *
129
+	 * @var array $collection_paths
130
+	 */
131
+	protected $collection_paths = array();
132
+
133
+	/**
134
+	 * @var LocatorInterface $file_locator
135
+	 */
136
+	protected $file_locator;
137
+
138
+
139
+	/**
140
+	 * CollectionDetails constructor.
141
+	 *
142
+	 * @access public
143
+	 * @param string           $collection_name
144
+	 * @param string           $collection_interface
145
+	 * @param array            $collection_FQCNs
146
+	 * @param array            $collection_paths
147
+	 * @param string           $file_mask
148
+	 * @param string           $identifier_type
149
+	 * @param string           $identifier_callback
150
+	 * @param LocatorInterface $file_locator
151
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
152
+	 * @throws \EventEspresso\core\exceptions\InvalidFilePathException
153
+	 * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
154
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
155
+	 * @throws \EventEspresso\core\exceptions\InvalidClassException
156
+	 */
157
+	public function __construct(
158
+		$collection_name,
159
+		$collection_interface,
160
+		$collection_FQCNs = array(),
161
+		$collection_paths = array(),
162
+		$file_mask = '',
163
+		$identifier_type = CollectionDetails::ID_OBJECT_HASH,
164
+		$identifier_callback = '',
165
+		LocatorInterface $file_locator = null
166
+	) {
167
+		$this->setCollectionName($collection_name);
168
+		$this->setCollectionInterface($collection_interface);
169
+		$this->setCollectionFQCNs($collection_FQCNs);
170
+		$this->setCollectionPaths($collection_paths);
171
+		$this->setFileMasks($file_mask);
172
+		$this->setIdentifierType($identifier_type);
173
+		$this->setIdentifierCallback($identifier_callback);
174
+		$this->file_locator = $file_locator;
175
+	}
176
+
177
+
178
+	/**
179
+	 * @access public
180
+	 * @return mixed
181
+	 */
182
+	public function getCollectionInterface()
183
+	{
184
+		return $this->collection_interface;
185
+	}
186
+
187
+
188
+	/**
189
+	 * @access protected
190
+	 * @param string $collection_interface
191
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
192
+	 */
193
+	protected function setCollectionInterface($collection_interface)
194
+	{
195
+		if (! (interface_exists($collection_interface) || class_exists($collection_interface))) {
196
+			throw new InvalidInterfaceException($collection_interface);
197
+		}
198
+		$this->collection_interface = $collection_interface;
199
+	}
200
+
201
+
202
+	/**
203
+	 * the collection name will be used for creating dynamic filters
204
+	 *
205
+	 * @access public
206
+	 * @return string
207
+	 */
208
+	public function collectionName()
209
+	{
210
+		return $this->collection_name;
211
+	}
212
+
213
+
214
+	/**
215
+	 * sanitizes collection name and converts spaces and dashes to underscores
216
+	 *
217
+	 * @access protected
218
+	 * @param string $collection_name
219
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
220
+	 */
221
+	protected function setCollectionName($collection_name)
222
+	{
223
+		if (! is_string($collection_name)) {
224
+			throw new InvalidDataTypeException('$collection_name', $collection_name, 'string');
225
+		}
226
+		$this->collection_name = str_replace(
227
+			'-',
228
+			'_',
229
+			sanitize_title_with_dashes($collection_name, '', 'save')
230
+		);
231
+	}
232
+
233
+
234
+	/**
235
+	 * @access public
236
+	 * @return string
237
+	 */
238
+	public function identifierType()
239
+	{
240
+		return $this->identifier_type;
241
+	}
242
+
243
+
244
+	/**
245
+	 * @access protected
246
+	 * @param string $identifier_type
247
+	 * @throws \EventEspresso\core\exceptions\InvalidIdentifierException
248
+	 */
249
+	protected function setIdentifierType($identifier_type)
250
+	{
251
+		if (! ($identifier_type === CollectionDetails::ID_CLASS_NAME
252
+			|| $identifier_type === CollectionDetails::ID_OBJECT_HASH
253
+			|| $identifier_type === CollectionDetails::ID_CALLBACK_METHOD
254
+		)) {
255
+			throw new InvalidIdentifierException(
256
+				$identifier_type,
257
+				'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD'
258
+			);
259
+		}
260
+		$this->identifier_type = $identifier_type;
261
+	}
262
+
263
+
264
+	/**
265
+	 * @access public
266
+	 * @return string
267
+	 */
268
+	public function identifierCallback()
269
+	{
270
+		return $this->identifier_callback;
271
+	}
272
+
273
+
274
+	/**
275
+	 * @access protected
276
+	 * @param string $identifier_callback
277
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
278
+	 */
279
+	protected function setIdentifierCallback($identifier_callback = 'identifier')
280
+	{
281
+		if (! is_string($identifier_callback)) {
282
+			throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string');
283
+		}
284
+		$this->identifier_callback = $identifier_callback;
285
+	}
286
+
287
+
288
+	/**
289
+	 * @access public
290
+	 * @return string
291
+	 */
292
+	public function getFileMask()
293
+	{
294
+		return $this->file_mask;
295
+	}
296
+
297
+
298
+	/**
299
+	 * sets the file mask which is then used to filter what files get loaded
300
+	 * when searching for classes to add to the collection. Defaults to '*.php'
301
+	 *
302
+	 * @access protected
303
+	 * @param string $file_mask
304
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
305
+	 */
306
+	protected function setFileMasks($file_mask)
307
+	{
308
+		$this->file_mask = ! empty($file_mask) ? $file_mask : '*.php';
309
+		// we know our default is a string, so if it's not a string now,
310
+		// then that means the incoming parameter was something else
311
+		if (! is_string($this->file_mask)) {
312
+			throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string');
313
+		}
314
+	}
315
+
316
+
317
+	/**
318
+	 * @access public
319
+	 * @return string
320
+	 */
321
+	public function getCollectionFQCNs()
322
+	{
323
+		return $this->collection_FQCNs;
324
+	}
325
+
326
+
327
+	/**
328
+	 * @access public
329
+	 * @param string|array $collection_FQCNs
330
+	 * @throws \EventEspresso\core\exceptions\InvalidClassException
331
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
332
+	 */
333
+	public function setCollectionFQCNs($collection_FQCNs)
334
+	{
335
+		foreach ((array)$collection_FQCNs as $collection_FQCN) {
336
+			if (! empty($collection_FQCN)) {
337
+				if (class_exists($collection_FQCN)) {
338
+					$this->collection_FQCNs[] = $collection_FQCN;
339
+				} else {
340
+					foreach ($this->getFQCNsFromPartialNamespace($collection_FQCN) as $FQCN) {
341
+						$this->collection_FQCNs[] = $FQCN;
342
+					}
343
+				}
344
+			}
345
+		}
346
+	}
347
+
348
+
349
+	/**
350
+	 * @access protected
351
+	 * @param  string $partial_FQCN
352
+	 * @return array
353
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
354
+	 * @throws \EventEspresso\core\exceptions\InvalidClassException
355
+	 */
356
+	protected function getFQCNsFromPartialNamespace($partial_FQCN)
357
+	{
358
+		if (! $this->file_locator instanceof FqcnLocator) {
359
+			$this->file_locator = new FqcnLocator();
360
+		}
361
+		$this->file_locator->locate($partial_FQCN);
362
+		return $this->file_locator->getFQCNs();
363
+	}
364
+
365
+
366
+	/**
367
+	 * @access public
368
+	 * @return string
369
+	 */
370
+	public function getCollectionPaths()
371
+	{
372
+		return $this->collection_paths;
373
+	}
374
+
375
+
376
+	/**
377
+	 * @access public
378
+	 * @param string|array $collection_paths
379
+	 * @throws \EventEspresso\core\exceptions\InvalidFilePathException
380
+	 */
381
+	public function setCollectionPaths($collection_paths)
382
+	{
383
+		foreach ((array)$collection_paths as $collection_path) {
384
+			if (! empty($collection_path)) {
385
+				if (! is_readable($collection_path)) {
386
+					throw new InvalidFilePathException($collection_path);
387
+				}
388
+				$this->collection_paths[] = $collection_path;
389
+			}
390
+		}
391
+	}
392 392
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -192,7 +192,7 @@  discard block
 block discarded – undo
192 192
      */
193 193
     protected function setCollectionInterface($collection_interface)
194 194
     {
195
-        if (! (interface_exists($collection_interface) || class_exists($collection_interface))) {
195
+        if ( ! (interface_exists($collection_interface) || class_exists($collection_interface))) {
196 196
             throw new InvalidInterfaceException($collection_interface);
197 197
         }
198 198
         $this->collection_interface = $collection_interface;
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
      */
221 221
     protected function setCollectionName($collection_name)
222 222
     {
223
-        if (! is_string($collection_name)) {
223
+        if ( ! is_string($collection_name)) {
224 224
             throw new InvalidDataTypeException('$collection_name', $collection_name, 'string');
225 225
         }
226 226
         $this->collection_name = str_replace(
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
      */
249 249
     protected function setIdentifierType($identifier_type)
250 250
     {
251
-        if (! ($identifier_type === CollectionDetails::ID_CLASS_NAME
251
+        if ( ! ($identifier_type === CollectionDetails::ID_CLASS_NAME
252 252
             || $identifier_type === CollectionDetails::ID_OBJECT_HASH
253 253
             || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD
254 254
         )) {
@@ -278,7 +278,7 @@  discard block
 block discarded – undo
278 278
      */
279 279
     protected function setIdentifierCallback($identifier_callback = 'identifier')
280 280
     {
281
-        if (! is_string($identifier_callback)) {
281
+        if ( ! is_string($identifier_callback)) {
282 282
             throw new InvalidDataTypeException('$identifier_callback', $identifier_callback, 'string');
283 283
         }
284 284
         $this->identifier_callback = $identifier_callback;
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
         $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php';
309 309
         // we know our default is a string, so if it's not a string now,
310 310
         // then that means the incoming parameter was something else
311
-        if (! is_string($this->file_mask)) {
311
+        if ( ! is_string($this->file_mask)) {
312 312
             throw new InvalidDataTypeException('$file_mask', $this->file_mask, 'string');
313 313
         }
314 314
     }
@@ -332,8 +332,8 @@  discard block
 block discarded – undo
332 332
      */
333 333
     public function setCollectionFQCNs($collection_FQCNs)
334 334
     {
335
-        foreach ((array)$collection_FQCNs as $collection_FQCN) {
336
-            if (! empty($collection_FQCN)) {
335
+        foreach ((array) $collection_FQCNs as $collection_FQCN) {
336
+            if ( ! empty($collection_FQCN)) {
337 337
                 if (class_exists($collection_FQCN)) {
338 338
                     $this->collection_FQCNs[] = $collection_FQCN;
339 339
                 } else {
@@ -355,7 +355,7 @@  discard block
 block discarded – undo
355 355
      */
356 356
     protected function getFQCNsFromPartialNamespace($partial_FQCN)
357 357
     {
358
-        if (! $this->file_locator instanceof FqcnLocator) {
358
+        if ( ! $this->file_locator instanceof FqcnLocator) {
359 359
             $this->file_locator = new FqcnLocator();
360 360
         }
361 361
         $this->file_locator->locate($partial_FQCN);
@@ -380,9 +380,9 @@  discard block
 block discarded – undo
380 380
      */
381 381
     public function setCollectionPaths($collection_paths)
382 382
     {
383
-        foreach ((array)$collection_paths as $collection_path) {
384
-            if (! empty($collection_path)) {
385
-                if (! is_readable($collection_path)) {
383
+        foreach ((array) $collection_paths as $collection_path) {
384
+            if ( ! empty($collection_path)) {
385
+                if ( ! is_readable($collection_path)) {
386 386
                     throw new InvalidFilePathException($collection_path);
387 387
                 }
388 388
                 $this->collection_paths[] = $collection_path;
Please login to merge, or discard this patch.
core/services/cache/BasicCacheManager.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -16,135 +16,135 @@  discard block
 block discarded – undo
16 16
 class BasicCacheManager implements CacheManagerInterface
17 17
 {
18 18
 
19
-    /**
20
-     * @type string
21
-     */
22
-    const CACHE_PREFIX = 'ee_cache_';
23
-
24
-
25
-    /**
26
-     * @var CacheStorageInterface $cache_storage
27
-     */
28
-    private $cache_storage;
29
-
30
-
31
-    /**
32
-     * BasicCacheManager constructor.
33
-     *
34
-     * @param CacheStorageInterface $cache_storage [required]
35
-     */
36
-    public function __construct(CacheStorageInterface $cache_storage)
37
-    {
38
-        $this->cache_storage = $cache_storage;
39
-    }
40
-
41
-
42
-    /**
43
-     * returns a string that will be prepended to all cache identifiers
44
-     *
45
-     * @return string
46
-     */
47
-    public function cachePrefix()
48
-    {
49
-        return BasicCacheManager::CACHE_PREFIX;
50
-    }
51
-
52
-
53
-    /**
54
-     * @param string  $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types.
55
-     *                           May also be helpful to include an additional specific identifier,
56
-     *                           such as a post ID as part of the $id_prefix so that individual caches
57
-     *                           can be found and/or cleared. ex: "venue-28", or "shortcode-156".
58
-     *                           BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id.
59
-     * @param string  $cache_id  [required] Additional identifying details that make this cache unique.
60
-     *                           It is advisable to use some of the actual data
61
-     *                           that is used to generate the content being cached,
62
-     *                           in order to guarantee that the cache id is unique for that content.
63
-     *                           The cache id will be md5'd before usage to make it more db friendly,
64
-     *                           and the entire cache id string will be truncated to 190 characters.
65
-     * @param Closure $callback  [required] since the point of caching is to avoid generating content when not
66
-     *                           necessary,
67
-     *                           we wrap our content creation in a Closure so that it is not executed until needed.
68
-     * @param int     $expiration
69
-     * @return Closure|mixed
70
-     */
71
-    public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS)
72
-    {
73
-        $content = '';
74
-        $expiration = absint(
75
-            apply_filters(
76
-                'FHEE__CacheManager__get__cache_expiration',
77
-                $expiration,
78
-                $id_prefix,
79
-                $cache_id
80
-            )
81
-        );
82
-        $cache_id = $this->generateCacheIdentifier($id_prefix, $cache_id);
83
-        // is caching enabled for this content ?
84
-        if ($expiration) {
85
-            $content = $this->cache_storage->get($cache_id);
86
-        }
87
-        // any existing content ?
88
-        if (empty($content)) {
89
-            // nope! let's generate some new stuff
90
-            $content = $callback();
91
-            // save the new content if caching is enabled
92
-            if ($expiration) {
93
-                $this->cache_storage->add($cache_id, $content, $expiration);
94
-                if (EE_DEBUG) {
95
-                    $content .= $this->displayCacheNotice($cache_id, 'REFRESH CACHE');
96
-                }
97
-            }
98
-        } else {
99
-            if (EE_DEBUG) {
100
-                $content .= $this->displayCacheNotice($cache_id, 'CACHED CONTENT');
101
-            }
102
-        }
103
-        return $content;
104
-    }
105
-
106
-
107
-    /**
108
-     * Generates a unique identifier string for the cache
109
-     *
110
-     * @param string $id_prefix [required] see BasicCacheManager::get()
111
-     * @param string $cache_id  [required] see BasicCacheManager::get()
112
-     * @return string
113
-     */
114
-    private function generateCacheIdentifier($id_prefix, $cache_id)
115
-    {
116
-        // let's make the cached content unique for this "page"
117
-        $cache_id .= filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL);
118
-        // with these parameters
119
-        $cache_id .= filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_URL);
120
-        // then md5 the above to control it's length, add all of our prefixes, and truncate
121
-        return substr($this->cachePrefix() . $id_prefix . '-' . md5($cache_id), 0, 182);
122
-    }
123
-
124
-
125
-    /**
126
-     * @param array|string $cache_id [required] Could be an ID prefix affecting many caches
127
-     *                               or a specific ID targeting a single cache item
128
-     * @return void
129
-     */
130
-    public function clear($cache_id)
131
-    {
132
-        // ensure incoming arg is in an array
133
-        $cache_id = is_array($cache_id) ? $cache_id : array($cache_id);
134
-        // delete corresponding transients for the supplied id prefix
135
-        $this->cache_storage->deleteMany($cache_id);
136
-    }
137
-
138
-
139
-    /**
140
-     * @param array|string $cache_id [required] Could be an ID prefix affecting many caches
141
-     *                               or a specific ID targeting a single cache item
142
-     * @param string       $type
143
-     * @return string
144
-     */
145
-    private function displayCacheNotice($cache_id, $type)
146
-    {
147
-        return '
19
+	/**
20
+	 * @type string
21
+	 */
22
+	const CACHE_PREFIX = 'ee_cache_';
23
+
24
+
25
+	/**
26
+	 * @var CacheStorageInterface $cache_storage
27
+	 */
28
+	private $cache_storage;
29
+
30
+
31
+	/**
32
+	 * BasicCacheManager constructor.
33
+	 *
34
+	 * @param CacheStorageInterface $cache_storage [required]
35
+	 */
36
+	public function __construct(CacheStorageInterface $cache_storage)
37
+	{
38
+		$this->cache_storage = $cache_storage;
39
+	}
40
+
41
+
42
+	/**
43
+	 * returns a string that will be prepended to all cache identifiers
44
+	 *
45
+	 * @return string
46
+	 */
47
+	public function cachePrefix()
48
+	{
49
+		return BasicCacheManager::CACHE_PREFIX;
50
+	}
51
+
52
+
53
+	/**
54
+	 * @param string  $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types.
55
+	 *                           May also be helpful to include an additional specific identifier,
56
+	 *                           such as a post ID as part of the $id_prefix so that individual caches
57
+	 *                           can be found and/or cleared. ex: "venue-28", or "shortcode-156".
58
+	 *                           BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id.
59
+	 * @param string  $cache_id  [required] Additional identifying details that make this cache unique.
60
+	 *                           It is advisable to use some of the actual data
61
+	 *                           that is used to generate the content being cached,
62
+	 *                           in order to guarantee that the cache id is unique for that content.
63
+	 *                           The cache id will be md5'd before usage to make it more db friendly,
64
+	 *                           and the entire cache id string will be truncated to 190 characters.
65
+	 * @param Closure $callback  [required] since the point of caching is to avoid generating content when not
66
+	 *                           necessary,
67
+	 *                           we wrap our content creation in a Closure so that it is not executed until needed.
68
+	 * @param int     $expiration
69
+	 * @return Closure|mixed
70
+	 */
71
+	public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS)
72
+	{
73
+		$content = '';
74
+		$expiration = absint(
75
+			apply_filters(
76
+				'FHEE__CacheManager__get__cache_expiration',
77
+				$expiration,
78
+				$id_prefix,
79
+				$cache_id
80
+			)
81
+		);
82
+		$cache_id = $this->generateCacheIdentifier($id_prefix, $cache_id);
83
+		// is caching enabled for this content ?
84
+		if ($expiration) {
85
+			$content = $this->cache_storage->get($cache_id);
86
+		}
87
+		// any existing content ?
88
+		if (empty($content)) {
89
+			// nope! let's generate some new stuff
90
+			$content = $callback();
91
+			// save the new content if caching is enabled
92
+			if ($expiration) {
93
+				$this->cache_storage->add($cache_id, $content, $expiration);
94
+				if (EE_DEBUG) {
95
+					$content .= $this->displayCacheNotice($cache_id, 'REFRESH CACHE');
96
+				}
97
+			}
98
+		} else {
99
+			if (EE_DEBUG) {
100
+				$content .= $this->displayCacheNotice($cache_id, 'CACHED CONTENT');
101
+			}
102
+		}
103
+		return $content;
104
+	}
105
+
106
+
107
+	/**
108
+	 * Generates a unique identifier string for the cache
109
+	 *
110
+	 * @param string $id_prefix [required] see BasicCacheManager::get()
111
+	 * @param string $cache_id  [required] see BasicCacheManager::get()
112
+	 * @return string
113
+	 */
114
+	private function generateCacheIdentifier($id_prefix, $cache_id)
115
+	{
116
+		// let's make the cached content unique for this "page"
117
+		$cache_id .= filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL);
118
+		// with these parameters
119
+		$cache_id .= filter_input(INPUT_SERVER, 'QUERY_STRING', FILTER_SANITIZE_URL);
120
+		// then md5 the above to control it's length, add all of our prefixes, and truncate
121
+		return substr($this->cachePrefix() . $id_prefix . '-' . md5($cache_id), 0, 182);
122
+	}
123
+
124
+
125
+	/**
126
+	 * @param array|string $cache_id [required] Could be an ID prefix affecting many caches
127
+	 *                               or a specific ID targeting a single cache item
128
+	 * @return void
129
+	 */
130
+	public function clear($cache_id)
131
+	{
132
+		// ensure incoming arg is in an array
133
+		$cache_id = is_array($cache_id) ? $cache_id : array($cache_id);
134
+		// delete corresponding transients for the supplied id prefix
135
+		$this->cache_storage->deleteMany($cache_id);
136
+	}
137
+
138
+
139
+	/**
140
+	 * @param array|string $cache_id [required] Could be an ID prefix affecting many caches
141
+	 *                               or a specific ID targeting a single cache item
142
+	 * @param string       $type
143
+	 * @return string
144
+	 */
145
+	private function displayCacheNotice($cache_id, $type)
146
+	{
147
+		return '
148 148
 <div class="ee-cached-content-notice" style="position:fixed; bottom:0; left: 0;">
149 149
     <p style="font-size:9px;font-weight:normal;color:#666;line-height: 12px;margin:0 0 3px 5px">
150 150
         <b>' . $type . '</b><span style="color:#999"> : </span>
@@ -152,5 +152,5 @@  discard block
 block discarded – undo
152 152
         <span style="margin-left:2em;">' . __FILE__ . '</span>
153 153
     </p>
154 154
 </div>';
155
-    }
155
+	}
156 156
 }
Please login to merge, or discard this patch.