| 1 |
<?php |
| 2 |
/** |
| 3 |
* A group of classes and methods to create and manage shortcodes. |
| 4 |
* |
| 5 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 6 |
* @copyright (c) 2018, Webcraftic Ltd |
| 7 |
* |
| 8 |
* @package core |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if( !defined('ABSPATH') ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if( !class_exists('Wbcr_FactoryShortcodes329') ) { |
| 18 |
/** |
| 19 |
* A helper class to register new shortcodes. |
| 20 |
* |
| 21 |
* One shortcode manager for all shortcodes from different plugins. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Wbcr_FactoryShortcodes329 { |
| 26 |
|
| 27 |
private static $manager = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* Registering a new shortcode. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public static function register($class_name, $plugin) |
| 36 |
{ |
| 37 |
if( !self::$manager ) { |
| 38 |
self::$manager = new Wbcr_FactoryShortcodes329_ShortcodeManager(); |
| 39 |
} |
| 40 |
self::$manager->register($class_name, $plugin); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if( !class_exists('Wbcr_FactoryShortcodes329_ShortcodeManager') ) { |
| 46 |
/** |
| 47 |
* Factory Shortcode Manager |
| 48 |
* |
| 49 |
* The main tasks of the manager is: |
| 50 |
* - creating aninstance of Factory Shortcode per every call of the shortcode. |
| 51 |
* - tracking shortcodes in post content. |
| 52 |
*/ |
| 53 |
class Wbcr_FactoryShortcodes329_ShortcodeManager { |
| 54 |
|
| 55 |
/** |
| 56 |
* A set of registered shortcodes. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
* @var FactoryShortcodes329_Shortcode[] |
| 60 |
*/ |
| 61 |
private $shortcodes = array(); |
| 62 |
|
| 63 |
/** |
| 64 |
* Keeps links between "class name" => "plugin" |
| 65 |
* |
| 66 |
* @since 3.2.0 |
| 67 |
* @var Wbcr_FactoryShortcodes329_Shortcode[] |
| 68 |
*/ |
| 69 |
private $class_to_plugin = array(); |
| 70 |
|
| 71 |
/** |
| 72 |
* A set of shortcodes that has assets connects (js and css). |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
* @var mixed[] |
| 76 |
*/ |
| 77 |
public $connected = array(); |
| 78 |
|
| 79 |
/** |
| 80 |
* This method allows to create a new shortcode object for each call. |
| 81 |
* |
| 82 |
* @param string $name A shortcode name. |
| 83 |
* @param array $arguments |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
public function __call($name, $arguments) |
| 87 |
{ |
| 88 |
list($prefix, $type) = explode('_', $name, 2); |
| 89 |
|
| 90 |
if( $prefix !== 'shortcode' ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$blank = new $type($this->class_to_plugin[$type]); |
| 95 |
|
| 96 |
return $blank->render($arguments[0], $arguments[1], $arguments[2]); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Registers a new shortcode. |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* @param string $class_name A short code class name. |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function register($class_name, $plugin) |
| 107 |
{ |
| 108 |
$shortcode = new $class_name($plugin); |
| 109 |
$shortcode->manager = $this; |
| 110 |
|
| 111 |
$this->shortcodes[] = $shortcode; |
| 112 |
|
| 113 |
foreach($shortcode->shortcode_name as $shortcode_name) { |
| 114 |
$class_name = get_class($shortcode); |
| 115 |
|
| 116 |
$this->class_to_plugin[$class_name] = $plugin; |
| 117 |
|
| 118 |
add_shortcode($shortcode_name, array($this, 'shortcode_' . $class_name)); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |