Factory.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM Shortcode |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Shortcode_Factory { |
| 17 | |
| 18 | /** |
| 19 | * |
| 20 | * @var type |
| 21 | */ |
| 22 | protected $strategy = null; |
| 23 | |
| 24 | /** |
| 25 | * Initialize shortcode factory |
| 26 | * |
| 27 | * @param type $args |
| 28 | * @param type $content |
| 29 | */ |
| 30 | public function __construct($args, $content) { |
| 31 | $context = !empty($args['context']) ? $args['context'] : 'content'; |
| 32 | |
| 33 | $classname = 'AAM_Shortcode_Strategy_' . ucfirst($context); |
| 34 | |
| 35 | if (class_exists($classname)) { |
| 36 | $this->strategy = new $classname($args, $content); |
| 37 | } else { |
| 38 | $this->strategy = apply_filters( |
| 39 | 'aam-shortcode-filter', null, $context, $args, $content |
| 40 | ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | public function process() { |
| 49 | if (is_a($this->strategy, 'AAM_Shortcode_Strategy_Interface')) { |
| 50 | $content = $this->strategy->run(); |
| 51 | } else { |
| 52 | $content = __('No valid strategy found for the given context', AAM_KEY); |
| 53 | } |
| 54 | |
| 55 | return $content; |
| 56 | } |
| 57 | |
| 58 | } |