-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 858f926
Showing
8 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 fahmifitu | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<br /> | ||
<div align="left"> | ||
<a href="https://github.com/fahmifitu/element-context-yt"> | ||
<img src="images/logo.png" alt="element-context-yt" width="80" height="80"> | ||
</a> | ||
|
||
<h3 align="left">Element Context YOOtheme Pro</h3> | ||
|
||
<p align="left"> | ||
Configure your YOOtheme elements to show and hide on certain sections of your site. | ||
<br /> | ||
<br /> | ||
<a href="https://github.com/fahmifitu/element-context-yt/issues">Report Bug</a> | ||
· | ||
<a href="https://github.com/fahmifitu/element-context-yt/issues">Request Feature</a> | ||
</p> | ||
</div> | ||
|
||
### Details | ||
This YOOtheme WordPress plugin available on GitHub introduces "Element Context" settings section to your existing elements. This feature allows you to control the visibility of elements based on several conditions. You can easily configure your elements to show or hide based on multiple rules, such as WordPress-specific locations such as the front page, blog page, archive, singles, search results, and 404 error pages. Additionally, you can control the visibility of elements based on language, custom post type archives and singles, taxonomy archives, and custom URLs. This plugin is particularly useful when you want to add multiple builder elements that you want to conditionally show or hide to widget areas such as top or bottom. For instance, you can use it to manage multiple footer elements, multiple before footer elements, or breadcrumbs. | ||
|
||
Feel free to contribute for more ideas of rules. | ||
### Dependencies | ||
|
||
- YOOtheme Pro | ||
- php >= 7.4.0 | ||
|
||
### Installation | ||
|
||
- Install plugin to your wordpress website | ||
|
||
### Author | ||
|
||
[Fahmi Elfituri](https://www.linkedin.com/in/fahmifitu) | ||
|
||
### Acknowledgments | ||
|
||
- [Widget Context plugin](https://wordpress.org/plugins/widget-context/) | ||
- [YOOtheme](https://yootheme.com/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace ElementContextYT; | ||
|
||
use YOOtheme\Builder; | ||
|
||
include_once __DIR__ . '/src/ContextTransform.php'; | ||
include_once __DIR__ . '/src/EventsListener.php'; | ||
|
||
return [ | ||
'events' => [ | ||
'builder.type' => [ | ||
EventsListener::class => [ | ||
['@onBuilderType'], | ||
], | ||
], | ||
'customizer.init' => [ | ||
EventsListener::class => [ | ||
['@onCustomizerInit'], | ||
] | ||
], | ||
], | ||
'extend' => [ | ||
Builder::class => function (Builder $builder, $app) { | ||
if (!(apply_filters('element_context_yt_disable', false))) { | ||
$builder->addTransform('render', $app(ContextTransform::class)); | ||
} | ||
} | ||
] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* Element Context YOOtheme | ||
* | ||
* @package Element Context YOOtheme | ||
* @author Fahmi Elfituri | ||
* | ||
* @wordpress-plugin | ||
* Plugin Name: Element Context YOOtheme | ||
* Description: Configure your YOOtheme elements to show and hide on certain sections of your site. | ||
* Version: 1.0.0 | ||
* Author: Fahmi Elfituri | ||
*/ | ||
|
||
define('ELEMENT_CONTEXT_YT_PATH', dirname(__FILE__)); | ||
|
||
use YOOtheme\Application; | ||
|
||
add_action('after_setup_theme', function () { | ||
// Check if YOOtheme Pro is loaded | ||
if (!class_exists(Application::class, false)) { | ||
return; | ||
} | ||
$app = Application::getInstance(); | ||
$app->load(__DIR__ . '/bootstrap.php'); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
<?php | ||
|
||
namespace ElementContextYT; | ||
|
||
if (!defined('ABSPATH')) exit; | ||
|
||
class ContextTransform | ||
{ | ||
public function __invoke($node) | ||
{ | ||
$props = $node->props; | ||
if (!isset($props['element_context_yt'])) { | ||
return; | ||
} | ||
|
||
$element_context_yt = $props['element_context_yt']; | ||
if ($element_context_yt->context === 'all') { | ||
return true; | ||
} | ||
if ($element_context_yt->context === 'none') { | ||
return false; | ||
} | ||
|
||
$languageMatched = !empty($element_context_yt->match_language) | ||
? $this->matchLanguage($element_context_yt->match_language) : | ||
true; | ||
$locationMatched = !empty($element_context_yt->match_location) | ||
? $this->matchLocations($element_context_yt->match_location) | ||
: false; | ||
|
||
$archivePostMatched = !empty($element_context_yt->match_archive_postype) | ||
? $this->matchArchivePost($element_context_yt->match_archive_postype) | ||
: false; | ||
$singlePostMatched = !empty($element_context_yt->match_single_postype) | ||
? $this->matchSinglePost($element_context_yt->match_single_postype) | ||
: false; | ||
$archiveTaxMatched = !empty($element_context_yt->match_archive_taxonomy) | ||
? $this->matchArchiveTax($element_context_yt->match_archive_taxonomy) | ||
: false; | ||
$urlMatched = !empty($element_context_yt->match_url) | ||
? $this->matchUrl($element_context_yt->match_url) | ||
: false; | ||
|
||
$isMatched = $languageMatched && ($locationMatched || $archivePostMatched || $singlePostMatched || $archiveTaxMatched || $urlMatched); | ||
if ($element_context_yt->context === 'show_selected') { | ||
return $isMatched; | ||
} | ||
|
||
if ($element_context_yt->context === 'hide_selected') { | ||
return !$isMatched; | ||
} | ||
} | ||
private function matchLanguage($language) | ||
{ | ||
$match_string = str_replace(' ', '', $language); | ||
$match_array = explode(',', $match_string); | ||
|
||
foreach ($match_array as $language) { | ||
if (get_locale() === $language) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
private function matchLocations($locations) | ||
{ | ||
foreach ($locations as $location) { | ||
if ($this->checkLocation($location)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
private function checkLocation($location) | ||
{ | ||
switch ($location) { | ||
case 'is_front_page': | ||
return is_front_page(); | ||
case 'is_home': | ||
return is_home(); | ||
case 'is_singular': | ||
return is_singular(); | ||
case 'is_single': | ||
return is_singular('post'); | ||
case 'is_page': | ||
return (is_page() && !is_front_page()); | ||
case 'is_attachment': | ||
return is_attachment(); | ||
case 'is_search': | ||
return is_search(); | ||
case 'is_404': | ||
return is_404(); | ||
case 'is_archive': | ||
return is_archive(); | ||
case 'is_date': | ||
return is_date(); | ||
case 'is_day': | ||
return is_day(); | ||
case 'is_month': | ||
return is_month(); | ||
case 'is_year': | ||
return is_year(); | ||
case 'is_category': | ||
return is_category(); | ||
case 'is_tag': | ||
return is_tag(); | ||
case 'is_author': | ||
return is_author(); | ||
} | ||
} | ||
private function matchArchivePost($posts) | ||
{ | ||
$match_string = str_replace(' ', '', $posts); | ||
$match_array = explode(',', $match_string); | ||
|
||
foreach ($match_array as $post) { | ||
if (is_post_type_archive($post)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
private function matchSinglePost($posts) | ||
{ | ||
$match_string = str_replace(' ', '', $posts); | ||
$match_array = explode(',', $match_string); | ||
|
||
foreach ($match_array as $post) { | ||
if (is_singular($post)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
private function matchArchiveTax($taxonomies) | ||
{ | ||
$match_string = str_replace(' ', '', $taxonomies); | ||
$match_array = explode(',', $match_string); | ||
|
||
foreach ($match_array as $tax) { | ||
if (is_tax($tax)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
private function matchUrl($urls) | ||
{ | ||
$path = $this->get_request_path(); | ||
if ($this->match_path($path, $urls)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
private function has_rules_with_query_strings($rules) | ||
{ | ||
foreach ($rules as $rule) { | ||
if (false !== strpos($rule, '=')) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
private function match_path($path, $rules) | ||
{ | ||
$uri_rules_paths = array_map('trim', $this->uri_rules_from_paths($rules)); | ||
if (!$this->has_rules_with_query_strings($uri_rules_paths)) { | ||
$path = strtok($path, '?'); | ||
} | ||
|
||
if (!empty($uri_rules_paths)) { | ||
return $this->uri_matches_rules($path, $uri_rules_paths); | ||
} | ||
|
||
return false; | ||
} | ||
private function uri_rules_from_paths($paths) | ||
{ | ||
$patterns = explode(",", $paths); | ||
|
||
$patterns = array_map( | ||
function ($pattern) { | ||
return $this->path_from_uri(trim($pattern)); | ||
}, | ||
$patterns | ||
); | ||
|
||
return array_filter($patterns); | ||
} | ||
private function get_request_path() | ||
{ | ||
static $path; | ||
if (!isset($path)) { | ||
$path = $this->path_from_uri($_SERVER['REQUEST_URI']); | ||
} | ||
return $path; | ||
} | ||
private function path_from_uri($uri) | ||
{ | ||
$parts = wp_parse_args( | ||
wp_parse_url($uri), | ||
array( | ||
'path' => '', | ||
) | ||
); | ||
|
||
$path = trim($parts['path'], '/'); | ||
|
||
if (!empty($parts['query'])) { | ||
$path .= '?' . $parts['query']; | ||
} | ||
|
||
return $path; | ||
} | ||
private function quote_rules($rules) | ||
{ | ||
return array_map( | ||
function ($rule) { | ||
$rule = preg_quote($rule, '/'); | ||
return str_replace(array_keys(['\*' => '.*']), ['\*' => '.*'], $rule); | ||
}, | ||
$rules | ||
); | ||
} | ||
private function rules_to_expression($rules) | ||
{ | ||
$rules = array_map( | ||
function ($rule) { | ||
return sprintf('(%s$)', $rule); | ||
}, | ||
$this->quote_rules($rules) | ||
); | ||
|
||
return sprintf('%s^(%s)%si', '/', implode('|', $rules), '/'); | ||
} | ||
private function uri_matches_rules($uri, $rules) | ||
{ | ||
if (!empty($rules)) { | ||
return (bool) preg_match($this->rules_to_expression($rules), $uri); | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.