1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: Adrien
5+ * Date: 20/11/2018
6+ * Time: 16:37
7+ */
8+
9+ namespace Tofandel \Core \Objects ;
10+
11+ use Tofandel \Core \ShortcodeMappers \VC_Mapper ;
12+
13+ class ShortcodeDescriptor {
14+ protected $ name ;
15+
16+ protected $ title ;
17+ protected $ description ;
18+ protected $ category ;
19+ protected $ icon ;
20+
21+ /**
22+ * @var ShortcodeParameter[]
23+ */
24+ protected $ parameters = array ();
25+
26+ protected $ currentParam ;
27+
28+ protected $ moreOptions ;
29+
30+ /**
31+ * ShortcodeDescriptor constructor.
32+ *
33+ * @param $class
34+ */
35+ public function __construct ( $ class ) {
36+ ShortcodeParameter::$ autoOrder = 0 ;
37+ $ this ->name = call_user_func ( array ( $ class , 'getName ' ) );
38+ }
39+
40+ public function getName () {
41+ return $ this ->name ;
42+ }
43+
44+ public function setInfo ( $ title , $ description = '' , $ category = '' , $ icon = '' ) {
45+ $ this ->title = $ title ;
46+ $ this ->description = $ description ;
47+ $ this ->category = $ category ;
48+ $ this ->icon = $ icon ;
49+ }
50+
51+ public function getTitle () {
52+ return $ this ->title ;
53+ }
54+
55+ public function getDescription () {
56+ return $ this ->description ;
57+ }
58+
59+ public function getCategory () {
60+ return $ this ->category ;
61+ }
62+
63+ public function getIcon () {
64+ return $ this ->icon ;
65+ }
66+
67+ public function __get ( $ name ) {
68+ return isset ( $ this ->moreOptions [ $ name ] ) ? $ this ->moreOptions [ $ name ] : null ;
69+ }
70+
71+ public function __set ( $ name , $ value ) {
72+ $ this ->moreOptions [ $ name ] = $ value ;
73+ }
74+
75+ public function __isset ( $ name ) {
76+ return isset ( $ this ->moreOptions [ $ name ] );
77+ }
78+
79+ public function addHTMLParameters () {
80+ global $ WPlusPlusCore ;
81+ $ this ->addParameter ( 'html_id ' , __ ( 'ID ' , $ WPlusPlusCore ->getTextDomain () ), ShortcodeParametersTypes::TEXT , __ ( 'The HTML id of the element ' , $ WPlusPlusCore ->getTextDomain () ), __ ( 'HTML Attributes ' , $ WPlusPlusCore ->getTextDomain () ) );
82+ $ this ->addParameter ( 'html_class ' , __ ( 'Class ' , $ WPlusPlusCore ->getTextDomain () ), ShortcodeParametersTypes::TEXT , __ ( 'The HTML class you want to add to the element ' , $ WPlusPlusCore ->getTextDomain () ), __ ( 'HTML Attributes ' , $ WPlusPlusCore ->getTextDomain () ) );
83+ return $ this ;
84+ }
85+
86+ /**
87+ * @param string|ShortcodeParameter $name
88+ * @param string $title
89+ * @param string $type ShortcodeParametersTypes::const
90+ * @param string $description
91+ * @param string $category
92+ *
93+ * @return ShortcodeParameter
94+ */
95+ public function addParameter ( $ name , $ title = '' , $ type = '' , $ description = '' , $ category = '' ) {
96+ if ( is_a ( $ name , ShortcodeParameter::class ) ) {
97+ $ this ->parameters [ $ name ->getName () ] = $ name ;
98+ } else {
99+ $ this ->parameters [ $ name ] = new ShortcodeParameter ( $ name , $ title , $ type , $ description , $ category );
100+ $ this ->currentParam = $ name ;
101+ }
102+
103+ return $ this ->parameters [ $ name ];
104+ }
105+
106+ public function parseRequest ( $ req ) {
107+ $ parsed_req = array ();
108+ foreach ( $ req as $ k => $ v ) {
109+ if ( isset ( $ this ->parameters [ $ k ] ) ) {
110+ $ parsed_req [ $ k ] = $ v ;
111+ }
112+ }
113+
114+ return $ parsed_req ;
115+ }
116+
117+ /**
118+ * Selects and returns a field or the current field
119+ *
120+ * @param string $paramName
121+ *
122+ * @return mixed|null
123+ */
124+ public function param ( $ paramName = '' ) {
125+ if ( ! empty ( $ paramName ) ) {
126+ $ this ->currentParam = $ paramName ;
127+ }
128+
129+ if ( isset ( $ this ->parameters [ $ this ->currentParam ] ) ) {
130+ return $ this ->parameters [ $ this ->currentParam ];
131+ } else {
132+ return null ;
133+ }
134+ }
135+
136+ public function removeParameter ( $ name ) {
137+ unset( $ this ->parameters [ $ name ] );
138+ }
139+
140+ protected function sortFields () {
141+ usort ( $ this ->parameters , function ( ShortcodeParameter $ item1 , ShortcodeParameter $ item2 ) {
142+ return $ item1 ->getOrder () <=> $ item2 ->getOrder ();
143+ } );
144+ }
145+
146+ /**
147+ * @var ShortcodeMapper[]
148+ */
149+ static $ mappers ;
150+
151+ /**
152+ * @return ShortcodeMapper[]
153+ */
154+ public static function initMappers () {
155+ static $ mappers ;
156+
157+ if ( ! isset ( $ mappers ) ) {
158+ self ::$ mappers = apply_filters ( 'wpp_shortcode_mappers ' , array ( VC_Mapper::class ) );
159+ foreach ( self ::$ mappers as $ mapper ) {
160+ if ( $ mapper ::shouldMap () ) {
161+ $ mappers [] = $ mapper ;
162+ }
163+ }
164+ }
165+
166+ return $ mappers ;
167+ }
168+ }
0 commit comments