| @@ -1,0 +1,819 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Extension Abstract | |
| 5 | + * | |
| 6 | + * @package NotificationX\Extensions | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace NotificationX\Extensions; | |
| 10 | + | |
| 11 | +use NotificationX\Admin\Cron; | |
| 12 | +use NotificationX\NotificationX; | |
| 13 | +use NotificationX\Admin\Entries; | |
| 14 | +use NotificationX\Admin\Settings; | |
| 15 | +use NotificationX\Core\Database; | |
| 16 | +use NotificationX\Core\Helper; | |
| 17 | +use NotificationX\Core\Modules; | |
| 18 | +use NotificationX\Core\PostType; | |
| 19 | +use NotificationX\Core\Rules; | |
| 20 | +use NotificationX\Core\Themes; | |
| 21 | +use NotificationX\Types\TypeFactory; | |
| 22 | +use NotificationX\Types\Types; | |
| 23 | +use NotificationX\Core\Limiter; | |
| 24 | + | |
| 25 | +/** | |
| 26 | + * Extension Abstract for all Extension. | |
| 27 | + */ | |
| 28 | +abstract class Extension { | |
| 29 | + | |
| 30 | + public $id; | |
| 31 | + public $title; | |
| 32 | + public $img = ''; | |
| 33 | + public $doc_link = 'https://notificationx.com/docs/'; | |
| 34 | + public $types = ''; | |
| 35 | + public $themes = []; | |
| 36 | + public $res_themes = []; | |
| 37 | + public $selected_themes = []; | |
| 38 | + public $is_pro = false; | |
| 39 | + public $popup = null; | |
| 40 | + public $module = ''; | |
| 41 | + public $module_title = ''; | |
| 42 | + public $version = ''; | |
| 43 | + public $class = ''; | |
| 44 | + public $function = ''; | |
| 45 | + public $constant = ''; | |
| 46 | + public $templates = []; | |
| 47 | + public $mobile_templates = []; | |
| 48 | + public $cron_schedule = ''; | |
| 49 | + public $exclude_custom_themes = false; | |
| 50 | + public $priority = 5; | |
| 51 | + public $module_priority = 5; | |
| 52 | + public $show_on_module = true; | |
| 53 | + public $show_on_type = true; | |
| 54 | + /** | |
| 55 | + * All Active Notification Items | |
| 56 | + * | |
| 57 | + * @var array | |
| 58 | + */ | |
| 59 | + public $enabled_types = []; | |
| 60 | + public $default_theme = ''; | |
| 61 | + public $link_type = ''; | |
| 62 | + | |
| 63 | + // @todo Something | |
| 64 | + // public abstract function save_post($post_id, $post, $update); | |
| 65 | + // public abstract function get_notification_ready($type, $data = array()); | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Initially Invoked when initialized. | |
| 69 | + * | |
| 70 | + * @hook init; Called in init hook. | |
| 71 | + */ | |
| 72 | + public function __construct() { | |
| 73 | + $modules = Modules::get_instance(); | |
| 74 | + $module_name = $this->register_module(); | |
| 75 | + if ($modules->is_enabled($module_name)) { | |
| 76 | + $type_factory = TypeFactory::get_instance(); | |
| 77 | + if( $this->show_on_type ) { | |
| 78 | + $type_factory->register_types($this->types); | |
| 79 | + } | |
| 80 | + $this->initialize(); | |
| 81 | + } | |
| 82 | + add_action('init', [$this, '__init_extension']); | |
| 83 | + } | |
| 84 | + | |
| 85 | + public function initialize(){ | |
| 86 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 87 | + do_action('nx::extension::init', $this); | |
| 88 | + add_action('nx_before_metabox_load', [$this, '__init_fields']); | |
| 89 | + add_action('nx_before_settings_fields', [$this, 'init_settings_fields']); | |
| 90 | + | |
| 91 | + if($this->is_active(false)) { | |
| 92 | + $this->init(); | |
| 93 | + $this->admin_actions(); | |
| 94 | + $this->public_actions(); | |
| 95 | + if(did_action('wpml_st_loaded')){ | |
| 96 | + $this->wpml_actions(); | |
| 97 | + } | |
| 98 | + add_action('nx_before_metabox_load', [$this, 'init_fields']); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * common init function for admin and frontend. | |
| 104 | + */ | |
| 105 | + public function init(){ | |
| 106 | + // shouldn't do is_active check. | |
| 107 | + if(method_exists($this, 'save_post')){ | |
| 108 | + add_filter("nx_save_post_{$this->id}", array($this, 'save_post'), 10, 3); | |
| 109 | + } | |
| 110 | + if(method_exists($this, 'saved_post')){ | |
| 111 | + add_filter("nx_saved_post_{$this->id}", array($this, 'saved_post'), 10, 3); | |
| 112 | + } | |
| 113 | + if(method_exists($this, 'preview_entry')){ | |
| 114 | + add_filter("nx_preview_entry_{$this->id}", array($this, 'preview_entry'), 10, 2); | |
| 115 | + } | |
| 116 | + if(method_exists($this, 'preview_settings')){ | |
| 117 | + add_filter("nx_preview_settings_{$this->id}", array($this, 'preview_settings'), 10, 2); | |
| 118 | + } | |
| 119 | + add_filter("nx_saved_post_{$this->id}", array($this, 'add_cron_job'), 15, 3); | |
| 120 | + } | |
| 121 | + | |
| 122 | + public function __init_extension() { | |
| 123 | + $this->init_extension(); | |
| 124 | + if( $this->show_on_module ) { | |
| 125 | + Modules::get_instance()->update($this->module,'label',$this->module_title); | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + public function init_extension() {} | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * common init function for admin and frontend. | |
| 133 | + */ | |
| 134 | + public function init_settings_fields(){ | |
| 135 | + | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * common init function . | |
| 140 | + */ | |
| 141 | + public function __init_fields(){ | |
| 142 | + add_filter('nx_themes', [$this, '__nx_themes']); | |
| 143 | + add_filter('nx_res_themes', [$this, '__nx_res_themes']); | |
| 144 | + add_filter('nx_sources', [$this, '__nx_sources'], 10, 1); | |
| 145 | + add_filter('nx_link_types_dependency', [$this, '__link_types_dependency']); | |
| 146 | + add_filter('nx_notification_template', [$this, '__notification_template']); | |
| 147 | + add_filter('nx_notification_template_mobile', [$this, '__notification_mobile_template']); | |
| 148 | + add_filter('nx_notification_template_dependency', [$this, '__notification_template_dependency']); | |
| 149 | + add_filter('nx_notification_template_mobile_dependency', [$this, '__notification_template_mobile_dependency']); | |
| 150 | + add_filter('nx_source_trigger', [$this, '__source_trigger']); | |
| 151 | + add_filter('nx_themes_trigger', [$this, '__themes_trigger']); | |
| 152 | + add_filter('nx_themes_trigger_for_responsive', [$this, '__res_themes_trigger']); | |
| 153 | + add_filter('nx_is_pro_sources', [$this, '__is_pro_sources']); | |
| 154 | + | |
| 155 | + if(method_exists($this, 'doc')){ | |
| 156 | + add_filter('nx_instructions', [$this, 'nx_instructions']); | |
| 157 | + } | |
| 158 | + if(method_exists($this, 'source_error_message')){ | |
| 159 | + add_filter('source_error_message', [$this, 'source_error_message']); | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * common init function for admin and frontend. | |
| 165 | + */ | |
| 166 | + public function init_fields(){ | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Runs when modules is enabled. | |
| 171 | + * | |
| 172 | + * @return void | |
| 173 | + */ | |
| 174 | + public function public_actions() { | |
| 175 | + if (method_exists($this, 'fallback_data')) { | |
| 176 | + add_filter("nx_fallback_data_{$this->id}", array($this, 'fallback_data'), 11, 3); | |
| 177 | + } | |
| 178 | + if (method_exists($this, 'notification_image')) { | |
| 179 | + add_filter("nx_notification_image_{$this->id}", array($this, 'notification_image'), 10, 3); | |
| 180 | + } | |
| 181 | + | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Runs when modules is enabled. | |
| 186 | + * | |
| 187 | + * @return void | |
| 188 | + */ | |
| 189 | + public function admin_actions() { | |
| 190 | + // add_action('nx_get_conversions_ready', array($this, 'get_notification_ready'), 10, 2); | |
| 191 | + } | |
| 192 | + | |
| 193 | + public function wpml_actions(){ | |
| 194 | + | |
| 195 | + } | |
| 196 | + | |
| 197 | + public function get_link_type(){ | |
| 198 | + $link_type = $this->link_type; | |
| 199 | + if(empty($link_type)){ | |
| 200 | + $type = $this->get_type(); | |
| 201 | + if(!empty($type->link_type)){ | |
| 202 | + $link_type = $type->link_type; | |
| 203 | + } | |
| 204 | + } | |
| 205 | + if($link_type === '-1'){ | |
| 206 | + $link_type = ''; | |
| 207 | + } | |
| 208 | + return $link_type; | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Get themes for the extension. | |
| 213 | + * | |
| 214 | + * | |
| 215 | + * @param array $args Settings arguments. | |
| 216 | + * @return mixed | |
| 217 | + */ | |
| 218 | + public function __source_trigger($triggers){ | |
| 219 | + if(empty($this->default_theme)){ | |
| 220 | + $type = TypeFactory::get_instance()->get($this->types); | |
| 221 | + $this->default_theme = $type->default_theme; | |
| 222 | + } | |
| 223 | + if(!empty($this->default_theme)){ | |
| 224 | + $triggers[$this->id]['themes'] = "@themes:{$this->default_theme}"; | |
| 225 | + } | |
| 226 | + $triggers[$this->id]['position'] = "@position:bottom_left"; | |
| 227 | + | |
| 228 | + $link_type = $this->get_link_type(); | |
| 229 | + | |
| 230 | + if (!empty($link_type)){ | |
| 231 | + $triggers[$this->id]['link_type'] = "@link_type:{$link_type}"; | |
| 232 | + } | |
| 233 | + return $triggers; | |
| 234 | + } | |
| 235 | + | |
| 236 | + /** | |
| 237 | + * Runs when modules is enabled. | |
| 238 | + * | |
| 239 | + * @return void | |
| 240 | + */ | |
| 241 | + public function __nx_themes($themes) { | |
| 242 | + $_themes = $this->get_themes(); | |
| 243 | + | |
| 244 | + $i = 0; | |
| 245 | + if(is_array($_themes)){ | |
| 246 | + foreach ($_themes as $tname => $theme) { | |
| 247 | + if (empty($themes[$tname])) { | |
| 248 | + $themes[$tname] = [ | |
| 249 | + 'label' => $tname, | |
| 250 | + 'value' => $tname, | |
| 251 | + 'is_pro' => isset($theme['is_pro']) ? $theme['is_pro'] && ! NotificationX::is_pro() : null, | |
| 252 | + 'icon' => isset($theme['source']) ? $theme['source'] : $theme, | |
| 253 | + // @todo converts | |
| 254 | + // 'trigger' => isset($theme['template']) ? ['notification-template' => $theme['template']] : null, | |
| 255 | + ]; | |
| 256 | + if(!empty($theme['column'])){ | |
| 257 | + $themes[$tname]['column'] = $theme['column']; | |
| 258 | + } | |
| 259 | + if(!empty($theme['rules'])){ | |
| 260 | + $themes[$tname]['rules'] = $theme['rules']; | |
| 261 | + } | |
| 262 | + } | |
| 263 | + | |
| 264 | + $themes[$tname] = Rules::includes('source', $this->id, false, $themes[$tname]); | |
| 265 | + // $themes[$tname] = Rules::includes('type', $this->types, false, $themes[$tname]); | |
| 266 | + // $themes[$tname] = Rules::includes('i', [++$i], false, $themes[$tname]); | |
| 267 | + // $themes[$tname] = Rules::includes('i', [++$i], false, $themes[$tname]); | |
| 268 | + // $themes[$tname] = Rules::includes('i', [++$i], false, $themes[$tname]); | |
| 269 | + } | |
| 270 | + } | |
| 271 | + return $themes; | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 275 | + * Runs when modules is enabled. | |
| 276 | + * | |
| 277 | + * @return void | |
| 278 | + */ | |
| 279 | + public function __nx_res_themes($themes) { | |
| 280 | + $_themes = $this->get_res_themes(); | |
| 281 | + $i = 0; | |
| 282 | + if(is_array($_themes)){ | |
| 283 | + foreach ($_themes as $tname => $theme) { | |
| 284 | + if (empty($themes[$tname])) { | |
| 285 | + $themes[$tname] = [ | |
| 286 | + 'label' => $tname, | |
| 287 | + 'value' => $tname, | |
| 288 | + 'is_pro' => isset($theme['is_pro']) ? $theme['is_pro'] && ! NotificationX::is_pro() : null, | |
| 289 | + 'icon' => isset($theme['source']) ? $theme['source'] : $theme, | |
| 290 | + // @todo converts | |
| 291 | + // 'trigger' => isset($theme['template']) ? ['notification-template' => $theme['template']] : null, | |
| 292 | + ]; | |
| 293 | + if(!empty($theme['column'])){ | |
| 294 | + $themes[$tname]['column'] = $theme['column']; | |
| 295 | + } | |
| 296 | + } | |
| 297 | + $template = isset($theme['_template']) ? $theme['_template'] : ''; | |
| 298 | + if( $template ) { | |
| 299 | + $templates = $this->get_templates(); | |
| 300 | + $main_themes = isset( $templates[$template] ) ? (isset( $templates[$template]['_themes'] ) ? $templates[$template]['_themes'] : '') : '' ; | |
| 301 | + $themes[$tname] = Rules::includes('themes', $main_themes, false, $themes[$tname]); | |
| 302 | + } | |
| 303 | + $themes[$tname] = Rules::includes('source', $this->id, false, $themes[$tname]); | |
| 304 | + } | |
| 305 | + } | |
| 306 | + return $themes; | |
| 307 | + } | |
| 308 | + | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * Get themes for the extension. | |
| 312 | + * | |
| 313 | + * | |
| 314 | + * @param array $args Settings arguments. | |
| 315 | + * @return mixed | |
| 316 | + */ | |
| 317 | + public function __themes_trigger($triggers) { | |
| 318 | + $_themes = $this->get_themes(); | |
| 319 | + if (is_array($_themes)) { | |
| 320 | + foreach ($_themes as $tname => $theme) { | |
| 321 | + if(!empty($theme['template']) && $templates = $theme['template']){ | |
| 322 | + foreach ($templates as $key => $value) { | |
| 323 | + $t = "@notification-template.{$key}:{$value}"; | |
| 324 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 325 | + $triggers[$tname][] = $t; | |
| 326 | + } | |
| 327 | + } | |
| 328 | + } | |
| 329 | + if(empty($theme['defaults']['link_button'])){ | |
| 330 | + $t = "@link_button:false"; | |
| 331 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 332 | + $triggers[$tname][] = $t; | |
| 333 | + } | |
| 334 | + } | |
| 335 | + if(!empty($theme['defaults']) && $defaults = $theme['defaults']){ | |
| 336 | + foreach ($defaults as $key => $value) { | |
| 337 | + if(is_array($value) && empty($triggers[$tname][$key])){ | |
| 338 | + $triggers[$tname][$key] = $value; | |
| 339 | + } | |
| 340 | + else{ | |
| 341 | + $t = "@{$key}:{$value}"; | |
| 342 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 343 | + $triggers[$tname][] = $t; | |
| 344 | + } | |
| 345 | + } | |
| 346 | + } | |
| 347 | + } | |
| 348 | + if(!empty($theme['image_shape'])){ | |
| 349 | + $t = "@image_shape:{$theme['image_shape']}"; | |
| 350 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 351 | + $triggers[$tname][] = $t; | |
| 352 | + } | |
| 353 | + // default image shape for theme. | |
| 354 | + $t = "@image_shape_default:{$theme['image_shape']}"; | |
| 355 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 356 | + $triggers[$tname][] = $t; | |
| 357 | + } | |
| 358 | + } | |
| 359 | + if(!empty($theme['inline_location'])){ | |
| 360 | + $t = $theme['inline_location']; | |
| 361 | + if(empty($triggers[$tname]['inline_location'])){ | |
| 362 | + $triggers[$tname]['inline_location'] = $theme['inline_location']; | |
| 363 | + } | |
| 364 | + } | |
| 365 | + if(!empty($theme['show_notification_image'])){ | |
| 366 | + $t = "@show_notification_image:{$theme['show_notification_image']}"; | |
| 367 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])) | |
| 368 | + $triggers[$tname][] = $t; | |
| 369 | + } | |
| 370 | + } | |
| 371 | + } | |
| 372 | + return $triggers; | |
| 373 | + } | |
| 374 | + | |
| 375 | + /** | |
| 376 | + * Get responsive themes for the extension. | |
| 377 | + * | |
| 378 | + * | |
| 379 | + * @param array $args Settings arguments. | |
| 380 | + * @return mixed | |
| 381 | + */ | |
| 382 | + public function __res_themes_trigger($triggers) { | |
| 383 | + $_themes = $this->get_res_themes(); | |
| 384 | + if (is_array($_themes)) { | |
| 385 | + foreach ($_themes as $tname => $theme) { | |
| 386 | + if(!empty($theme['template']) && $templates = $theme['template']){ | |
| 387 | + foreach ($templates as $key => $value) { | |
| 388 | + $t = "@notification-template-mobile.{$key}:{$value}"; | |
| 389 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 390 | + $triggers[$tname][] = $t; | |
| 391 | + } | |
| 392 | + } | |
| 393 | + } | |
| 394 | + if(empty($theme['defaults']['link_button'])){ | |
| 395 | + $t = "@link_button:false"; | |
| 396 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 397 | + $triggers[$tname][] = $t; | |
| 398 | + } | |
| 399 | + } | |
| 400 | + if(!empty($theme['defaults']) && $defaults = $theme['defaults']){ | |
| 401 | + foreach ($defaults as $key => $value) { | |
| 402 | + if(is_array($value) && empty($triggers[$tname][$key])){ | |
| 403 | + $triggers[$tname][$key] = $value; | |
| 404 | + } | |
| 405 | + else{ | |
| 406 | + $t = "@{$key}:{$value}"; | |
| 407 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 408 | + $triggers[$tname][] = $t; | |
| 409 | + } | |
| 410 | + } | |
| 411 | + } | |
| 412 | + } | |
| 413 | + if(!empty($theme['image_shape'])){ | |
| 414 | + $t = "@image_shape:{$theme['image_shape']}"; | |
| 415 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 416 | + $triggers[$tname][] = $t; | |
| 417 | + } | |
| 418 | + // default image shape for theme. | |
| 419 | + $t = "@image_shape_default:{$theme['image_shape']}"; | |
| 420 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])){ | |
| 421 | + $triggers[$tname][] = $t; | |
| 422 | + } | |
| 423 | + } | |
| 424 | + if(!empty($theme['inline_location'])){ | |
| 425 | + $t = $theme['inline_location']; | |
| 426 | + if(empty($triggers[$tname]['inline_location'])){ | |
| 427 | + $triggers[$tname]['inline_location'] = $theme['inline_location']; | |
| 428 | + } | |
| 429 | + } | |
| 430 | + if(!empty($theme['show_notification_image'])){ | |
| 431 | + $t = "@show_notification_image:{$theme['show_notification_image']}"; | |
| 432 | + if(empty($triggers[$tname]) || !in_array($t, $triggers[$tname])) | |
| 433 | + $triggers[$tname][] = $t; | |
| 434 | + } | |
| 435 | + } | |
| 436 | + } | |
| 437 | + return $triggers; | |
| 438 | + } | |
| 439 | + | |
| 440 | + /** | |
| 441 | + * Runs when modules is enabled. | |
| 442 | + * | |
| 443 | + * @return void | |
| 444 | + */ | |
| 445 | + public function __nx_sources($sources) { | |
| 446 | + $sources[] = [ | |
| 447 | + 'rules' => ['is', 'type', $this->types], | |
| 448 | + 'label' => $this->title, | |
| 449 | + 'icon' => $this->img, | |
| 450 | + 'value' => $this->id, | |
| 451 | + 'is_pro' => $this->is_pro && ! NotificationX::is_pro(), | |
| 452 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 453 | + 'popup' => apply_filters('nx_pro_alert_popup', $this->popup), | |
| 454 | + 'priority' => $this->priority, | |
| 455 | + ]; | |
| 456 | + return $sources; | |
| 457 | + } | |
| 458 | + | |
| 459 | + /** | |
| 460 | + * Adds dependency if a option was added to link type field from Types. | |
| 461 | + * | |
| 462 | + * @param array $dependency | |
| 463 | + * @return array | |
| 464 | + */ | |
| 465 | + public function __link_types_dependency($dependency) { | |
| 466 | + $link_type = $this->get_link_type(); | |
| 467 | + if (!empty($link_type)){ | |
| 468 | + $dependency[] = $this->id; | |
| 469 | + } | |
| 470 | + return $dependency; | |
| 471 | + } | |
| 472 | + | |
| 473 | + /** | |
| 474 | + * Adds options and dependency for `Notification Template` field from `Content` tab. | |
| 475 | + * | |
| 476 | + * @param array $templates `Notification Template` fields. | |
| 477 | + * @return array | |
| 478 | + */ | |
| 479 | + public function __notification_template($templates){ | |
| 480 | + foreach ($this->get_templates() as $key => $tmpl) { | |
| 481 | + if(!empty($tmpl['_themes'])){ | |
| 482 | + $type = 'themes'; | |
| 483 | + $themes = $tmpl['_themes']; | |
| 484 | + unset($tmpl['_themes']); | |
| 485 | + } | |
| 486 | + elseif(!empty($tmpl['_source'])) { | |
| 487 | + $type = 'source'; | |
| 488 | + $themes = $tmpl['_source']; | |
| 489 | + unset($tmpl['_source']); | |
| 490 | + } | |
| 491 | + | |
| 492 | + foreach ($tmpl as $param => $options) { | |
| 493 | + foreach ($options as $name => $label) { | |
| 494 | + if(empty($templates[$param]['options'][$name])){ | |
| 495 | + if(is_array($label)){ | |
| 496 | + $templates[$param]['options'][$name] = $label; | |
| 497 | + } | |
| 498 | + else{ | |
| 499 | + $templates[$param]['options'][$name] = [ | |
| 500 | + 'value' => $name, | |
| 501 | + 'label' => $label, | |
| 502 | + 'rules' => [], | |
| 503 | + ]; | |
| 504 | + } | |
| 505 | + } | |
| 506 | + $templates[$param]['options'][$name] = Rules::includes($type, $themes, false, $templates[$param]['options'][$name]); | |
| 507 | + } | |
| 508 | + } | |
| 509 | + } | |
| 510 | + | |
| 511 | + if(is_array($this->get_themes())){ | |
| 512 | + foreach ($this->get_themes() as $name => $theme) { | |
| 513 | + if(!empty($theme['template'])){ | |
| 514 | + foreach ($theme['template'] as $param => $value) { | |
| 515 | + if(strpos($param, 'custom_') === 0 || empty($templates[$param])) continue; | |
| 516 | + $templates[$param] = Rules::includes('themes', [$name], false, $templates[$param]); | |
| 517 | + } | |
| 518 | + } | |
| 519 | + } | |
| 520 | + } | |
| 521 | + | |
| 522 | + return $templates; | |
| 523 | + } | |
| 524 | + | |
| 525 | + /** | |
| 526 | + * Adds options and dependency for `Notification Template` field from `Content` tab. | |
| 527 | + * | |
| 528 | + * @param array $templates `Notification Template` fields. | |
| 529 | + * @return array | |
| 530 | + */ | |
| 531 | + public function __notification_template_dependency($dependency){ | |
| 532 | + if($this->get_templates()){ | |
| 533 | + $dependency[] = $this->id; | |
| 534 | + } | |
| 535 | + return $dependency; | |
| 536 | + } | |
| 537 | + | |
| 538 | + public function get_type(){ | |
| 539 | + return TypeFactory::get_instance()->get($this->types); | |
| 540 | + } | |
| 541 | + | |
| 542 | + /** | |
| 543 | + * Get themes for the extension. | |
| 544 | + * | |
| 545 | + * | |
| 546 | + * @param array $args Settings arguments. | |
| 547 | + * @return mixed | |
| 548 | + */ | |
| 549 | + public function get_themes() { | |
| 550 | + if (empty($this->themes)) { | |
| 551 | + $type_obj = $this->get_type(); | |
| 552 | + if ($type_obj instanceof Types) { | |
| 553 | + $themes = $type_obj->get_themes(); | |
| 554 | + return $this->array_add_prefix($themes, $this->types . "_"); | |
| 555 | + } | |
| 556 | + } | |
| 557 | + return $this->array_add_prefix($this->themes, $this->id . "_"); | |
| 558 | + } | |
| 559 | + | |
| 560 | + /** | |
| 561 | + * Get responsive themes for the extension. | |
| 562 | + * | |
| 563 | + * | |
| 564 | + * @param array $args Settings arguments. | |
| 565 | + * @return mixed | |
| 566 | + */ | |
| 567 | + public function get_res_themes() { | |
| 568 | + if (empty($this->res_themes)) { | |
| 569 | + $type_obj = $this->get_type(); | |
| 570 | + if ($type_obj instanceof Types) { | |
| 571 | + $themes = $type_obj->get_res_themes(); | |
| 572 | + return $this->array_add_prefix($themes, $this->types . "_"); | |
| 573 | + } | |
| 574 | + } | |
| 575 | + return $this->array_add_prefix($this->res_themes, $this->id . "_"); | |
| 576 | + } | |
| 577 | + | |
| 578 | + /** | |
| 579 | + * Get themes for the extension. | |
| 580 | + * | |
| 581 | + * | |
| 582 | + * @param array $args Settings arguments. | |
| 583 | + * @return mixed | |
| 584 | + */ | |
| 585 | + public function get_themes_name() { | |
| 586 | + return array_keys($this->get_themes()); | |
| 587 | + } | |
| 588 | + | |
| 589 | + public function array_add_prefix($array, $prefix){ | |
| 590 | + return ! is_array( $array ) ? $array : array_combine( | |
| 591 | + array_map( function( $key ) use( $prefix ) { | |
| 592 | + return $prefix . $key; | |
| 593 | + }, array_keys( $array ) ), | |
| 594 | + $array | |
| 595 | + ); | |
| 596 | + } | |
| 597 | + | |
| 598 | + /** | |
| 599 | + * Get templates for the extension. | |
| 600 | + * | |
| 601 | + * | |
| 602 | + * @return array | |
| 603 | + */ | |
| 604 | + public function get_templates(){ | |
| 605 | + if(empty($this->templates)){ | |
| 606 | + $type_obj = $this->get_type(); | |
| 607 | + if ($type_obj instanceof Types) { | |
| 608 | + return $type_obj->get_templates(); | |
| 609 | + } | |
| 610 | + } | |
| 611 | + return $this->templates; | |
| 612 | + } | |
| 613 | + | |
| 614 | + /** | |
| 615 | + * Undocumented function | |
| 616 | + * | |
| 617 | + * @param [array] $modules | |
| 618 | + * @return array | |
| 619 | + */ | |
| 620 | + public function register_module() { | |
| 621 | + if( $this->show_on_module ) { | |
| 622 | + $modules = Modules::get_instance(); | |
| 623 | + return $modules->add(array( | |
| 624 | + 'value' => $this->module, | |
| 625 | + 'label' => $this->module_title, | |
| 626 | + 'link' => $this->doc_link, | |
| 627 | + 'is_pro' => $this->is_pro && ! NotificationX::is_pro(), | |
| 628 | + 'badge' => $this->is_pro, | |
| 629 | + 'priority' => $this->module_priority, | |
| 630 | + )); | |
| 631 | + } | |
| 632 | + | |
| 633 | + } | |
| 634 | + | |
| 635 | + public function delete_notification($entry_key = null, $nx_id = null) { | |
| 636 | + $where = [ | |
| 637 | + // 'source' => $this->id | |
| 638 | + ]; | |
| 639 | + if (!empty($entry_key)) { | |
| 640 | + $where['entry_key'] = $entry_key; | |
| 641 | + } | |
| 642 | + if (!empty($nx_id)) { | |
| 643 | + $where['nx_id'] = $nx_id; | |
| 644 | + } | |
| 645 | + if (!empty($where)) { | |
| 646 | + return Entries::get_instance()->delete_entries($where); | |
| 647 | + } | |
| 648 | + return false; | |
| 649 | + } | |
| 650 | + | |
| 651 | + // @todo accept multiple entries. | |
| 652 | + public function update_notifications($entries) { | |
| 653 | + if(is_array($entries) && !empty($entries[0]['nx_id'])){ | |
| 654 | + $post = PostType::get_instance()->get_post($entries[0]['nx_id']); | |
| 655 | + foreach ($entries as $key => $entry) { | |
| 656 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 657 | + $can_entry = apply_filters("nx_can_entry_{$this->id}", true, $entry, $post); | |
| 658 | + if(!$can_entry){ | |
| 659 | + unset($entries[$key]); | |
| 660 | + } | |
| 661 | + } | |
| 662 | + Limiter::get_instance()->remove($post['nx_id'], count($entries)); | |
| 663 | + Entries::get_instance()->insert_entries(array_values($entries)); | |
| 664 | + } | |
| 665 | + } | |
| 666 | + | |
| 667 | + // @todo Something | |
| 668 | + public function update_notification($entry, $force = true) { // , $nx_id = 0 | |
| 669 | + if (empty($entry['nx_id'])) { // empty($nx_id) && | |
| 670 | + $this->save($entry, $force); | |
| 671 | + } else { | |
| 672 | + if(!$force){ | |
| 673 | + $is_exits = Database::get_instance()->get_posts( | |
| 674 | + Database::$table_entries, 'count(*)', [ | |
| 675 | + 'nx_id' => $entry['nx_id'], | |
| 676 | + 'source' => $this->id, | |
| 677 | + 'entry_key' => $entry['entry_key'], | |
| 678 | + ] ); | |
| 679 | + if(!empty($is_exits[0]['count(*)'])) return false; | |
| 680 | + } | |
| 681 | + // @todo add object caching | |
| 682 | + $post = PostType::get_instance()->get_post($entry['nx_id']); | |
| 683 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 684 | + $can_entry = apply_filters("nx_can_entry_{$this->id}", true, $entry, $post); | |
| 685 | + if($can_entry){ | |
| 686 | + Limiter::get_instance()->remove($post['nx_id'], 1); | |
| 687 | + Entries::get_instance()->insert_entry($entry); | |
| 688 | + } | |
| 689 | + } | |
| 690 | + } | |
| 691 | + | |
| 692 | + /** | |
| 693 | + * This method is responsible for save the data | |
| 694 | + * | |
| 695 | + * @param string $this->id - notification type | |
| 696 | + * @param array $data - notification data to save. | |
| 697 | + * @return boolean | |
| 698 | + */ | |
| 699 | + protected function save($entry, $force = true) { | |
| 700 | + $posts = PostType::get_instance()->get_posts([ | |
| 701 | + 'source' => $this->id, | |
| 702 | + 'enabled' => true, | |
| 703 | + ]); | |
| 704 | + if (!empty($posts)) { | |
| 705 | + foreach ($posts as $post) { | |
| 706 | + $entry['nx_id'] = $post['nx_id']; | |
| 707 | + $this->update_notification($entry, $force); | |
| 708 | + } | |
| 709 | + } | |
| 710 | + } | |
| 711 | + | |
| 712 | + /** | |
| 713 | + * this function is responsible for check a type of notification is created or not | |
| 714 | + * | |
| 715 | + * @param string $type | |
| 716 | + * @return boolean | |
| 717 | + */ | |
| 718 | + public function is_active($check_enabled = true) { | |
| 719 | + | |
| 720 | + if (!empty($this->class) && !class_exists($this->class)) { | |
| 721 | + return false; | |
| 722 | + } | |
| 723 | + if (!empty($this->function) && !function_exists($this->function)) { | |
| 724 | + return false; | |
| 725 | + } | |
| 726 | + if (!empty($this->constant) && !defined($this->constant)) { | |
| 727 | + return false; | |
| 728 | + } | |
| 729 | + if ($check_enabled) { | |
| 730 | + $active_sources = PostType::get_instance()->get_active_items(); | |
| 731 | + if (!empty($active_sources)) { | |
| 732 | + return in_array($this->id, $active_sources); | |
| 733 | + } | |
| 734 | + return false; | |
| 735 | + } | |
| 736 | + return true; | |
| 737 | + } | |
| 738 | + | |
| 739 | + public function class_exists() { | |
| 740 | + if (!empty($this->class)) { | |
| 741 | + return class_exists($this->class); | |
| 742 | + } | |
| 743 | + if (!empty($this->function)) { | |
| 744 | + return function_exists($this->function); | |
| 745 | + } | |
| 746 | + if (!empty($this->constant)) { | |
| 747 | + return defined($this->constant); | |
| 748 | + } | |
| 749 | + return true; | |
| 750 | + } | |
| 751 | + | |
| 752 | + /** | |
| 753 | + * This method is responsible for save the data | |
| 754 | + * | |
| 755 | + * @param string $this->id - notification type | |
| 756 | + * @param array $data - notification data to save. | |
| 757 | + * @return boolean | |
| 758 | + */ | |
| 759 | + protected function notEmpty($key, $array, $default = null) { | |
| 760 | + if(!empty($array[$key])){ | |
| 761 | + return $array[$key]; | |
| 762 | + } | |
| 763 | + return $default; | |
| 764 | + } | |
| 765 | + | |
| 766 | + /** | |
| 767 | + * Generating Full Name with one letter from last name | |
| 768 | + * @since 1.3.9 | |
| 769 | + * @param string $first_name | |
| 770 | + * @param string $last_name | |
| 771 | + * @return string | |
| 772 | + */ | |
| 773 | + protected function name($first_name = '', $last_name = '') { | |
| 774 | + $name = Helper::name($first_name, $last_name); | |
| 775 | + return $name; | |
| 776 | + } | |
| 777 | + | |
| 778 | + public function remote_get($url, $args = array(), $raw = false) { | |
| 779 | + return Helper::remote_get($url, $args, $raw); | |
| 780 | + } | |
| 781 | + | |
| 782 | + /** | |
| 783 | + * This method is responsible for save the data | |
| 784 | + * | |
| 785 | + * @param string $type - notification type | |
| 786 | + * @param array $data - notification data to save. | |
| 787 | + * @return boolean | |
| 788 | + */ | |
| 789 | + protected function sort_data($arr) { | |
| 790 | + // @todo add sorting. | |
| 791 | + return $arr; | |
| 792 | + } | |
| 793 | + | |
| 794 | + /** | |
| 795 | + * Base REST response for extensions that do not implement one. | |
| 796 | + * Dispatched from REST::rest_response() via method_exists(). | |
| 797 | + */ | |
| 798 | + public function restResponse( $params ){ | |
| 799 | + } | |
| 800 | + | |
| 801 | + public function nx_instructions($instructions){ | |
| 802 | + if(method_exists($this, 'doc')){ | |
| 803 | + $instructions[$this->types][$this->id] = $this->doc(); | |
| 804 | + } | |
| 805 | + return $instructions; | |
| 806 | + } | |
| 807 | + | |
| 808 | + public function __is_pro_sources($sources){ | |
| 809 | + $sources[$this->id] = $this->is_pro; | |
| 810 | + return $sources; | |
| 811 | + } | |
| 812 | + | |
| 813 | + public function add_cron_job($post, $data, $nx_id){ | |
| 814 | + if(!empty($this->cron_schedule)){ | |
| 815 | + Cron::get_instance()->set_cron($nx_id, $this->cron_schedule); | |
| 816 | + } | |
| 817 | + } | |
| 818 | + | |
| 819 | +} | |