| @@ -1,23 +1,57 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | - | |
| 4 | -namespace Jet_Form_Builder\Shortcodes; | |
| 5 | - | |
| 6 | - | |
| 7 | -class Manager { | |
| 8 | - | |
| 9 | - public static $instance = null; | |
| 10 | - | |
| 11 | - public static function instance() { | |
| 12 | - if ( is_null( self::$instance ) ) { | |
| 13 | - self::$instance = new self(); | |
| 14 | - } | |
| 15 | - | |
| 16 | - return self::$instance; | |
| 17 | - } | |
| 18 | - | |
| 19 | - | |
| 20 | - private function __construct() { | |
| 21 | - new Form_Shortcode(); | |
| 22 | - } | |
| 23 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | + | |
| 4 | +namespace Jet_Form_Builder\Shortcodes; | |
| 5 | + | |
| 6 | +use Jet_Form_Builder\Classes\Instance_Trait; | |
| 7 | +use JFB_Components\Repository\Repository_Pattern_Trait; | |
| 8 | +use Jet_Form_Builder\Exceptions\Repository_Exception; | |
| 9 | + | |
| 10 | +// If this file is called directly, abort. | |
| 11 | +if ( ! defined( 'WPINC' ) ) { | |
| 12 | + die; | |
| 13 | +} | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * @method static Manager instance() | |
| 17 | + * | |
| 18 | + * Class Manager | |
| 19 | + * @package Jet_Form_Builder\Shortcodes | |
| 20 | + */ | |
| 21 | +class Manager { | |
| 22 | + | |
| 23 | + use Instance_Trait; | |
| 24 | + use Repository_Pattern_Trait; | |
| 25 | + | |
| 26 | + private function __construct() { | |
| 27 | + $this->rep_install(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public function rep_instances(): array { | |
| 31 | + return array( | |
| 32 | + new Form_Shortcode(), | |
| 33 | + ); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static function get_shortcode( $type, $arguments ): string { | |
| 37 | + $format = '[%1$s %2$s]'; | |
| 38 | + | |
| 39 | + try { | |
| 40 | + $type = self::instance()->rep_clone_item( $type ); | |
| 41 | + } catch ( Repository_Exception $exception ) { | |
| 42 | + return ''; | |
| 43 | + } | |
| 44 | + | |
| 45 | + return sprintf( $format, $type->get_name(), self::generate_arguments_string( $arguments ) ); | |
| 46 | + } | |
| 47 | + | |
| 48 | + public static function generate_arguments_string( $arguments ): string { | |
| 49 | + $response = array(); | |
| 50 | + | |
| 51 | + foreach ( $arguments as $name => $value ) { | |
| 52 | + $response[] = "$name=\"$value\""; | |
| 53 | + } | |
| 54 | + | |
| 55 | + return implode( ' ', $response ); | |
| 56 | + } | |
| 57 | +} | |