Version Description
(June 13, 2018) = - Fix URL matching for URLs with query strings. - Introduce unit tests for the URL context.
Download this release
Release Info
Developer | kasparsd |
Plugin | Widget Context |
Version | 1.1.0 |
Comparing to | |
See all releases |
Code changes from version 1.0.7 to 1.1.0
- class/class-widget-context.php +84 -30
- readme.txt +5 -1
- widget-context.php +2 -2
class/class-widget-context.php
CHANGED
@@ -391,7 +391,16 @@ class widget_context {
|
|
391 |
}
|
392 |
|
393 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
394 |
function context_check_url( $check, $settings ) {
|
|
|
395 |
|
396 |
$settings = wp_parse_args(
|
397 |
$settings,
|
@@ -402,57 +411,102 @@ class widget_context {
|
|
402 |
|
403 |
$urls = trim( $settings['urls'] );
|
404 |
|
405 |
-
if ( empty( $urls ) )
|
406 |
return $check;
|
|
|
407 |
|
408 |
-
if (
|
|
|
|
|
|
|
|
|
|
|
409 |
return true;
|
|
|
410 |
|
411 |
return $check;
|
412 |
-
|
413 |
}
|
414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
|
416 |
-
|
417 |
-
|
418 |
-
global $wp;
|
419 |
-
|
420 |
-
$patterns_safe = array();
|
421 |
-
|
422 |
-
// Get the request URI from WP
|
423 |
-
$url_request = $wp->request;
|
424 |
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
|
429 |
-
|
|
|
430 |
|
431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
432 |
|
433 |
-
|
434 |
-
|
|
|
|
|
435 |
|
436 |
-
|
437 |
-
|
438 |
|
439 |
-
|
440 |
-
|
|
|
|
|
441 |
|
442 |
-
|
|
|
443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
444 |
}
|
445 |
|
446 |
-
|
447 |
-
|
|
|
448 |
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
);
|
453 |
|
454 |
-
|
|
|
|
|
|
|
455 |
|
|
|
456 |
}
|
457 |
|
458 |
|
391 |
}
|
392 |
|
393 |
|
394 |
+
/**
|
395 |
+
* Conditional logic for the URL check.
|
396 |
+
*
|
397 |
+
* @param bool $check Current visibility state.
|
398 |
+
* @param array $settings Visibility settings.
|
399 |
+
*
|
400 |
+
* @return bool
|
401 |
+
*/
|
402 |
function context_check_url( $check, $settings ) {
|
403 |
+
static $path;
|
404 |
|
405 |
$settings = wp_parse_args(
|
406 |
$settings,
|
411 |
|
412 |
$urls = trim( $settings['urls'] );
|
413 |
|
414 |
+
if ( empty( $urls ) ) {
|
415 |
return $check;
|
416 |
+
}
|
417 |
|
418 |
+
if ( ! isset( $path ) ) {
|
419 |
+
// Do the parsing only once.
|
420 |
+
$path = $this->get_request_path( $_SERVER['REQUEST_URI'] );
|
421 |
+
}
|
422 |
+
|
423 |
+
if ( $this->match_path( $path, $urls ) ) {
|
424 |
return true;
|
425 |
+
}
|
426 |
|
427 |
return $check;
|
|
|
428 |
}
|
429 |
|
430 |
+
/**
|
431 |
+
* Return the path relative to the root of the hostname. We always remove
|
432 |
+
* the leading and trailing slashes around the URI path.
|
433 |
+
*
|
434 |
+
* @param string $uri Current request URI.
|
435 |
+
*
|
436 |
+
* @return string
|
437 |
+
*/
|
438 |
+
public function get_request_path( $uri ) {
|
439 |
+
$parts = wp_parse_args(
|
440 |
+
wp_parse_url( $uri ),
|
441 |
+
array(
|
442 |
+
'path' => '',
|
443 |
+
)
|
444 |
+
);
|
445 |
|
446 |
+
$path = trim( $parts['path'], '/' );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
447 |
|
448 |
+
if ( ! empty( $parts['query'] ) ) {
|
449 |
+
$path .= '?' . $parts['query'];
|
450 |
+
}
|
451 |
|
452 |
+
return $path;
|
453 |
+
}
|
454 |
|
455 |
+
/**
|
456 |
+
* Check if the current request matches path rules.
|
457 |
+
*
|
458 |
+
* @param string $path Current request relative to the root of the hostname.
|
459 |
+
* @param string $rules A list of path patterns seperated by new line.
|
460 |
+
*
|
461 |
+
* @return bool
|
462 |
+
*/
|
463 |
+
function match_path( $path, $rules ) {
|
464 |
+
$path_only = strtok( $path, '?' );
|
465 |
+
$patterns = explode( "\n", $rules );
|
466 |
|
467 |
+
foreach ( $patterns as &$pattern ) {
|
468 |
+
// Use the same logic for parsing the visibility rules.
|
469 |
+
$pattern = $this->get_request_path( trim( $pattern ) );
|
470 |
+
}
|
471 |
|
472 |
+
// Remove empty patterns.
|
473 |
+
$patterns = array_filter( $patterns );
|
474 |
|
475 |
+
// Match against the path with and without the query string.
|
476 |
+
if ( $this->path_matches_patterns( $path, $patterns ) || $this->path_matches_patterns( $path_only, $patterns ) ) {
|
477 |
+
return true;
|
478 |
+
}
|
479 |
|
480 |
+
return false;
|
481 |
+
}
|
482 |
|
483 |
+
/**
|
484 |
+
* Check if a URI path matches a set of regex patterns.
|
485 |
+
*
|
486 |
+
* @param string $path Request URI.
|
487 |
+
* @param array $patterns A list of patterns.
|
488 |
+
*
|
489 |
+
* @return bool
|
490 |
+
*/
|
491 |
+
public function path_matches_patterns( $path, $patterns ) {
|
492 |
+
if ( empty( $patterns ) ) {
|
493 |
+
return false;
|
494 |
}
|
495 |
|
496 |
+
foreach ( $patterns as &$pattern ) {
|
497 |
+
// Escape regex chars since we only support wildcards.
|
498 |
+
$pattern = preg_quote( trim( $pattern ), '/' );
|
499 |
|
500 |
+
// Enable wildcard checks.
|
501 |
+
$pattern = str_replace( '\*', '.*', $pattern );
|
502 |
+
}
|
|
|
503 |
|
504 |
+
$regex = sprintf(
|
505 |
+
'/^(%s)$/i',
|
506 |
+
implode( '|', $patterns )
|
507 |
+
);
|
508 |
|
509 |
+
return (bool) preg_match( $regex, $path );
|
510 |
}
|
511 |
|
512 |
|
readme.txt
CHANGED
@@ -4,7 +4,7 @@ Contributors: kasparsd, jamescollins
|
|
4 |
Tags: widget, widget context, context, logic, widget logic, cms
|
5 |
Requires at least: 3.0
|
6 |
Tested up to: 4.9.6
|
7 |
-
Stable tag: 1.0
|
8 |
License: GPLv2 or later
|
9 |
|
10 |
Show or hide widgets on specific posts, pages or sections of your site.
|
@@ -29,6 +29,10 @@ Widget Context allows you to show or hide widgets on certain sections of your si
|
|
29 |
|
30 |
== Changelog ==
|
31 |
|
|
|
|
|
|
|
|
|
32 |
= 1.0.7 (June 5, 2018) =
|
33 |
- Mark as tested with WordPress 4.9.6.
|
34 |
- Use the localisation service provided by [WP.org](https://translate.wordpress.org/projects/wp-plugins/widget-context).
|
4 |
Tags: widget, widget context, context, logic, widget logic, cms
|
5 |
Requires at least: 3.0
|
6 |
Tested up to: 4.9.6
|
7 |
+
Stable tag: 1.1.0
|
8 |
License: GPLv2 or later
|
9 |
|
10 |
Show or hide widgets on specific posts, pages or sections of your site.
|
29 |
|
30 |
== Changelog ==
|
31 |
|
32 |
+
= 1.1.0 (June 13, 2018) =
|
33 |
+
- Fix URL matching for URLs with query strings.
|
34 |
+
- Introduce unit tests for the URL context.
|
35 |
+
|
36 |
= 1.0.7 (June 5, 2018) =
|
37 |
- Mark as tested with WordPress 4.9.6.
|
38 |
- Use the localisation service provided by [WP.org](https://translate.wordpress.org/projects/wp-plugins/widget-context).
|
widget-context.php
CHANGED
@@ -3,13 +3,13 @@
|
|
3 |
* Plugin Name: Widget Context
|
4 |
* Plugin URI: https://widgetcontext.com
|
5 |
* Description: Show or hide widgets depending on the section of the site that is being viewed.
|
6 |
-
* Version: 1.0
|
7 |
* Author: Kaspars Dambis
|
8 |
* Author URI: https://kaspars.net
|
9 |
* Text Domain: widget-context
|
10 |
*/
|
11 |
|
12 |
-
|
13 |
|
14 |
// Go!
|
15 |
$plugin = widget_context::instance();
|
3 |
* Plugin Name: Widget Context
|
4 |
* Plugin URI: https://widgetcontext.com
|
5 |
* Description: Show or hide widgets depending on the section of the site that is being viewed.
|
6 |
+
* Version: 1.1.0
|
7 |
* Author: Kaspars Dambis
|
8 |
* Author URI: https://kaspars.net
|
9 |
* Text Domain: widget-context
|
10 |
*/
|
11 |
|
12 |
+
require_once dirname( __FILE__ ) . '/class/class-widget-context.php';
|
13 |
|
14 |
// Go!
|
15 |
$plugin = widget_context::instance();
|