| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.5 |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base\Conditions; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Base\Functions; |
| 20 |
use FPFramework\Libs\Registry; |
| 21 |
use FPFramework\Helpers\StringHelper; |
| 22 |
|
| 23 |
abstract class PluginBase extends Condition |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Category/Taxonomy. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $taxonomy = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* The component's Single Page view name |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $postTypeSingle = 'post'; |
| 38 |
|
| 39 |
protected $request = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* Class constructor |
| 43 |
* |
| 44 |
* @param array $options The rule options. Expected properties: selection, value, params |
| 45 |
* @param object $factory The framework's factory class. |
| 46 |
*/ |
| 47 |
public function __construct($options = null, $factory = null) |
| 48 |
{ |
| 49 |
parent::__construct($options, $factory); |
| 50 |
|
| 51 |
$this->request = $this->getRequest(); |
| 52 |
} |
| 53 |
|
| 54 |
private function getRequest() |
| 55 |
{ |
| 56 |
$object = get_queried_object(); |
| 57 |
|
| 58 |
$request = new \stdClass; |
| 59 |
|
| 60 |
$request->id = $object instanceof \WP_Term ? (int) $object->term_id : ($object instanceof \WP_Post ? (int) $object->ID : null); |
| 61 |
$request->post_type = isset($object->post_type) ? $object->post_type : null; |
| 62 |
$request->taxonomy = $object && $object instanceof \WP_Term ? $object->taxonomy : null; |
| 63 |
|
| 64 |
return $request; |
| 65 |
} |
| 66 |
/** |
| 67 |
* Base assignment check |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
public function pass() |
| 72 |
{ |
| 73 |
return $this->passByOperator(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Indicates whether the current view concerncs a Single Page view |
| 78 |
* |
| 79 |
* @return boolean |
| 80 |
*/ |
| 81 |
public function isSinglePage() |
| 82 |
{ |
| 83 |
return ($this->request->post_type === $this->postTypeSingle); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Indicates whether the current view concerns a Category view |
| 88 |
* |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
protected function isCategoryPage() |
| 92 |
{ |
| 93 |
return $this->request->taxonomy === $this->taxonomy; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Check whether this page passes the validation |
| 98 |
* |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
protected function passSinglePage() |
| 102 |
{ |
| 103 |
if (empty($this->selection) || !$this->isSinglePage()) |
| 104 |
{ |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
if (!is_array($this->selection)) |
| 109 |
{ |
| 110 |
$this->selection = Functions::makeArray($this->selection); |
| 111 |
} |
| 112 |
|
| 113 |
return $this->passByOperator(); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Checks whether the current page is within the selected categories |
| 118 |
* |
| 119 |
* @return boolean |
| 120 |
*/ |
| 121 |
protected function passCategories() |
| 122 |
{ |
| 123 |
if (empty($this->selection)) |
| 124 |
{ |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
// Include Children switch: 0 = No, 1 = Yes, 2 = Child Only |
| 129 |
$inc_children = $this->params->get('inc_children'); |
| 130 |
|
| 131 |
// Setup supported views |
| 132 |
$view_single = $this->params->get('view_single', true); |
| 133 |
$view_category = $this->params->get('view_category', false); |
| 134 |
|
| 135 |
// Check if we are in a valid context |
| 136 |
if (!($view_category && $this->isCategoryPage()) && !($view_single && $this->isSinglePage())) |
| 137 |
{ |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
// Start Checks |
| 142 |
$pass = false; |
| 143 |
|
| 144 |
// Get current page assosiated category IDs. It can be a single ID of the current Category view or multiple IDs assosiated to active item. |
| 145 |
$catids = $this->getCategoryIds(); |
| 146 |
$catids = is_array($catids) ? $catids : (array) $catids; |
| 147 |
|
| 148 |
foreach ($catids as $catid) |
| 149 |
{ |
| 150 |
$pass = in_array($catid, $this->selection); |
| 151 |
|
| 152 |
if ($pass) |
| 153 |
{ |
| 154 |
// If inc_children is either disabled or set to 'Also on Childs', there's no need for further checks. |
| 155 |
// The condition is already passed. |
| 156 |
if (in_array($this->params->get('inc_children'), [0, 1])) |
| 157 |
{ |
| 158 |
break; |
| 159 |
} |
| 160 |
|
| 161 |
// We are here because we need childs only. Disable pass and continue checking parent IDs. |
| 162 |
$pass = false; |
| 163 |
} |
| 164 |
|
| 165 |
// Pass check for child items |
| 166 |
if (!$pass && $this->params->get('inc_children')) |
| 167 |
{ |
| 168 |
// Get parent categories |
| 169 |
$parent_ids = get_ancestors($catid, $this->taxonomy); |
| 170 |
|
| 171 |
foreach ($parent_ids as $id) |
| 172 |
{ |
| 173 |
if (in_array($id, $this->selection)) |
| 174 |
{ |
| 175 |
$pass = true; |
| 176 |
break 2; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
unset($parent_ids); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return $pass; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns category IDs based |
| 189 |
* |
| 190 |
* @return array |
| 191 |
*/ |
| 192 |
protected function getCategoryIDs() |
| 193 |
{ |
| 194 |
$id = $this->request->id; |
| 195 |
|
| 196 |
// Make sure we have an ID. |
| 197 |
if (empty($id)) |
| 198 |
{ |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// If this is a Category page, return the Category ID from the Query String |
| 203 |
if ($this->isCategoryPage()) |
| 204 |
{ |
| 205 |
return (array) $id; |
| 206 |
} |
| 207 |
|
| 208 |
// If this is a Single Page, return all assosiated Category IDs. |
| 209 |
if ($this->isSinglePage()) |
| 210 |
{ |
| 211 |
return $this->getSinglePageCategories($id); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get single page's assosiated categories |
| 217 |
* |
| 218 |
* @param Integer The Single Page id |
| 219 |
* |
| 220 |
* @return array |
| 221 |
*/ |
| 222 |
abstract protected function getSinglePageCategories($id); |
| 223 |
} |