PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/js_composer/include/classes/core/class-wpb-map.php

https://gitlab.com/oxidigitaluser/liguelista
PHP | 563 lines | 302 code | 56 blank | 205 comment | 58 complexity | 25ce5124142525588c1327c8cb697ca1 MD5 | raw file
  1. <?php
  2. /**
  3. * WPBakery Visual Composer Main manager.
  4. *
  5. * @package WPBakeryVisualComposer
  6. * @since 4.2
  7. */
  8. /**
  9. * Vc mapper new class.
  10. *
  11. * This class maps shortcodes settings to VC editors. You can manage add new shortcodes or manage default shortcodes
  12. * mapped in config/map.php. For developers it is possible to use API functions to add update settings attributes.
  13. *
  14. * @see config/map.php
  15. * @see include/helpers/helpers_api.php
  16. * @since 3.1
  17. *
  18. */
  19. class WPBMap {
  20. /**
  21. * @var array
  22. */
  23. protected static $sc = Array();
  24. /**
  25. * @var array
  26. */
  27. protected static $categories = Array();
  28. /**
  29. * @var bool
  30. */
  31. protected static $user_sc = false;
  32. /**
  33. * @var bool
  34. */
  35. protected static $user_sorted_sc = false;
  36. /**
  37. * @var bool
  38. */
  39. protected static $user_categories = false;
  40. /**
  41. * @var Vc_Settings $settings
  42. */
  43. protected static $settings;
  44. /**
  45. * @var
  46. */
  47. protected static $user_role;
  48. /**
  49. * @var
  50. */
  51. protected static $tags_regexp;
  52. /**
  53. * @var bool
  54. */
  55. protected static $is_init = false;
  56. /**
  57. * Set init status fro WPMap.
  58. *
  59. * if $is_init is FALSE, then all activity like add, update and delete for shortcodes attributes will be hold in
  60. * the list of activity and will be executed after initialization.
  61. *
  62. * @see Vc_Mapper::iniy.
  63. * @static
  64. *
  65. * @param bool $value
  66. */
  67. public static function setInit( $value = true ) {
  68. self::$is_init = $value;
  69. }
  70. /**
  71. * Gets user role and access rules for current user.
  72. *
  73. * @static
  74. * @return mixed
  75. */
  76. protected static function getSettings() {
  77. global $current_user;
  78. if ( self::$settings === null ) {
  79. if ( function_exists( 'get_currentuserinfo' ) ) {
  80. get_currentuserinfo();
  81. /** @var Vc_Settings $settings - get use group access rules */
  82. if ( ! empty( $current_user->roles ) ) {
  83. self::$user_role = $current_user->roles[0];
  84. } else {
  85. self::$user_role = 'author';
  86. }
  87. } else {
  88. self::$user_role = 'author';
  89. }
  90. self::$settings = vc_settings()->get( 'groups_access_rules' );
  91. }
  92. return self::$settings;
  93. }
  94. /**
  95. * Check is shortcode with a tag mapped to VC.
  96. *
  97. * @static
  98. *
  99. * @param $tag - shortcode tag.
  100. *
  101. * @return bool
  102. */
  103. public static function exists( $tag ) {
  104. return (boolean) isset( self::$sc[ $tag ] );
  105. }
  106. /**
  107. * Map shortcode to VC.
  108. *
  109. * This method maps shortcode to VC.
  110. * You need to shortcode's tag and settings to map correctly.
  111. * Default shortcodes are mapped in config/map.php file.
  112. * The best way is to call this method with "init" action callback function of WP.
  113. *
  114. * vc_filter: vc_mapper_tag - to change shortcode tag, arguments 2 ( $tag, $attributes )
  115. * vc_filter: vc_mapper_attributes - to change shortcode attributes (like params array), arguments 2 ( $attributes, $tag )
  116. * vc_filter: vc_mapper_attribute - to change singe shortcode param data, arguments 2 ( $attribute, $tag )
  117. * vc_filter: vc_mapper_attribute_{PARAM_TYPE} - to change singe shortcode param data by param type, arguments 2 ( $attribute, $tag )
  118. *
  119. * @static
  120. *
  121. * @param $tag
  122. * @param $attributes
  123. *
  124. * @return bool
  125. */
  126. public static function map( $tag, $attributes ) {
  127. if ( ! self::$is_init ) {
  128. if ( empty( $attributes['name'] ) ) {
  129. trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $tag ) );
  130. } elseif ( empty( $attributes['base'] ) ) {
  131. trigger_error( sprintf( __( "Wrong base for shortcode:%s. Base required", "js_composer" ), $tag ) );
  132. } else {
  133. vc_mapper()->addActivity(
  134. 'mapper', 'map', array(
  135. 'tag' => $tag,
  136. 'attributes' => $attributes
  137. )
  138. );
  139. return true;
  140. }
  141. return false;
  142. }
  143. if ( empty( $attributes['name'] ) ) {
  144. trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $tag ) );
  145. } elseif ( empty( $attributes['base'] ) ) {
  146. trigger_error( sprintf( __( "Wrong base for shortcode:%s. Base required", "js_composer" ), $tag ) );
  147. } else {
  148. self::$sc[ $tag ] = $attributes;
  149. self::$sc[ $tag ]['params'] = Array();
  150. if ( ! empty( $attributes['params'] ) ) {
  151. foreach ( $attributes['params'] as $attribute ) {
  152. $attribute = apply_filters( 'vc_mapper_attribute', $attribute, $tag );
  153. $attribute = apply_filters( 'vc_mapper_attribute_' . $attribute['type'], $attribute, $tag );
  154. self::$sc[ $tag ]['params'][] = $attribute;
  155. }
  156. $sort = new Vc_Sort( self::$sc[ $tag ]['params'] );
  157. self::$sc[ $tag ]['params'] = $sort->sortByKey();
  158. }
  159. visual_composer()->addShortCode( self::$sc[ $tag ] );
  160. return true;
  161. }
  162. return false;
  163. }
  164. /**
  165. * Generates list of shortcodes taking into account the access rules for shortcodes from VC Settings page.
  166. *
  167. * This method parses the list of mapped shortcodes and creates categories list for users.
  168. *
  169. * @static
  170. *
  171. * @param bool $force - force data generation even data already generated.
  172. */
  173. protected static function generateUserData( $force = false ) {
  174. if ( ! $force && self::$user_sc !== false && self::$user_categories !== false ) {
  175. return;
  176. }
  177. $settings = self::getSettings();
  178. self::$user_sc = self::$user_categories = self::$user_sorted_sc = array();
  179. foreach ( self::$sc as $name => $values ) {
  180. if ( in_array( $name, array(
  181. 'vc_column',
  182. 'vc_row',
  183. 'vc_row_inner',
  184. 'vc_column_inner'
  185. ) ) || ! isset( $settings[ self::$user_role ]['shortcodes'] )
  186. || ( isset( $settings[ self::$user_role ]['shortcodes'][ $name ] ) && (int) $settings[ self::$user_role ]['shortcodes'][ $name ] == 1 )
  187. ) {
  188. if ( ! isset( $values['content_element'] ) || $values['content_element'] === true ) {
  189. $categories = isset( $values['category'] ) ? $values['category'] : '_other_category_';
  190. $values['_category_ids'] = array();
  191. if ( is_array( $categories ) ) {
  192. foreach ( $categories as $c ) {
  193. if ( array_search( $c, self::$user_categories ) === false ) {
  194. self::$user_categories[] = $c;
  195. }
  196. $values['_category_ids'][] = md5( $c ); // array_search($category, self::$categories);
  197. }
  198. } else {
  199. if ( array_search( $categories, self::$user_categories ) === false ) {
  200. self::$user_categories[] = $categories;
  201. }
  202. $values['_category_ids'][] = md5( $categories ); // array_search($category, self::$categories);
  203. }
  204. }
  205. self::$user_sc[ $name ] = $values;
  206. self::$user_sorted_sc[] = $values;
  207. }
  208. }
  209. $sort = new Vc_Sort( self::$user_sorted_sc );
  210. self::$user_sorted_sc = $sort->sortByKey();
  211. }
  212. /**
  213. * Get mapped shortcode settings.
  214. *
  215. * @static
  216. * @return array
  217. */
  218. public static function getShortCodes() {
  219. return self::$sc;
  220. }
  221. /**
  222. * Get sorted list of mapped shortcode settings for current user.
  223. *
  224. * Sorting depends on the weight attribute and mapping order.
  225. *
  226. * @static
  227. * @return array
  228. */
  229. public static function getSortedUserShortCodes() {
  230. self::generateUserData();
  231. return self::$user_sorted_sc;
  232. }
  233. /**
  234. * Get list of mapped shortcode settings for current user.
  235. * @static
  236. * @return array - associated array of shortcodes settings with tag as the key.
  237. */
  238. public static function getUserShortCodes() {
  239. self::generateUserData();
  240. return self::$user_sc;
  241. }
  242. /**
  243. * Get mapped shortcode settings by tag.
  244. *
  245. * @static
  246. *
  247. * @param $tag - shortcode tag.
  248. *
  249. * @return array|null null @since 4.4.3
  250. */
  251. public static function getShortCode( $tag ) {
  252. return isset(self::$sc[ $tag ]) && is_array(self::$sc[ $tag ]) ? self::$sc[ $tag ] : null;
  253. }
  254. /**
  255. * Get all categories for mapped shortcodes.
  256. *
  257. * @static
  258. * @return array
  259. */
  260. public static function getCategories() {
  261. return self::$categories;
  262. }
  263. /**
  264. * Get all categories for current user.
  265. *
  266. * Category is added to the list when at least one shortcode of this category is allowed for current user
  267. * by Vc access rules.
  268. *
  269. * @static
  270. * @return array
  271. */
  272. public static function getUserCategories() {
  273. self::generateUserData();
  274. return self::$user_categories;
  275. }
  276. /**
  277. * Drop shortcode param.
  278. *
  279. * @static
  280. *
  281. * @param $name
  282. * @param $attribute_name
  283. *
  284. * @return bool
  285. */
  286. public static function dropParam( $name, $attribute_name ) {
  287. if ( ! self::$is_init ) {
  288. vc_mapper()->addActivity(
  289. 'mapper', 'drop_param', array(
  290. 'name' => $name,
  291. 'attribute_name' => $attribute_name
  292. )
  293. );
  294. return true;
  295. }
  296. foreach ( self::$sc[ $name ]['params'] as $index => $param ) {
  297. if ( $param['param_name'] == $attribute_name ) {
  298. array_splice( self::$sc[ $name ]['params'], $index, 1 );
  299. return true;
  300. }
  301. }
  302. return true;
  303. }
  304. /**
  305. * Returns param settings for mapped shortcodes.
  306. *
  307. * @static
  308. *
  309. * @param $tag
  310. * @param $param_name
  311. *
  312. * @return bool| array
  313. */
  314. public static function getParam( $tag, $param_name ) {
  315. if ( ! isset( self::$sc[ $tag ] ) ) {
  316. return trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $tag ) );
  317. }
  318. foreach ( self::$sc[ $tag ]['params'] as $index => $param ) {
  319. if ( $param['param_name'] == $param_name ) {
  320. return self::$sc[ $tag ]['params'][ $index ];
  321. }
  322. }
  323. return false;
  324. }
  325. /**
  326. * Add new param to shortcode params list.
  327. *
  328. * @static
  329. *
  330. * @param $name
  331. * @param array $attribute
  332. *
  333. * @return bool - true if added, false if scheduled/rejected
  334. */
  335. public static function addParam( $name, $attribute = Array() ) {
  336. if ( ! self::$is_init ) {
  337. vc_mapper()->addActivity(
  338. 'mapper', 'add_param', array(
  339. 'name' => $name,
  340. 'attribute' => $attribute
  341. )
  342. );
  343. return false;
  344. }
  345. if ( ! isset( self::$sc[ $name ] ) ) {
  346. trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $name ) );
  347. } elseif ( ! isset( $attribute['param_name'] ) ) {
  348. trigger_error( sprintf( __( "Wrong attribute for '%s' shortcode. Attribute 'param_name' required", "js_composer" ), $name ) );
  349. } else {
  350. $replaced = false;
  351. foreach ( self::$sc[ $name ]['params'] as $index => $param ) {
  352. if ( $param['param_name'] == $attribute['param_name'] ) {
  353. $replaced = true;
  354. self::$sc[ $name ]['params'][ $index ] = $attribute;
  355. }
  356. }
  357. if ( $replaced === false ) {
  358. self::$sc[ $name ]['params'][] = $attribute;
  359. }
  360. $sort = new Vc_Sort( self::$sc[ $name ]['params'] );
  361. self::$sc[ $name ]['params'] = $sort->sortByKey();
  362. visual_composer()->addShortCode( self::$sc[ $name ] );
  363. return true;
  364. }
  365. return false;
  366. }
  367. /**
  368. * Change param attributes of mapped shortcode.
  369. *
  370. * @static
  371. *
  372. * @param $name
  373. * @param array $attribute
  374. *
  375. * @return bool
  376. */
  377. public static function mutateParam( $name, $attribute = Array() ) {
  378. if ( ! self::$is_init ) {
  379. vc_mapper()->addActivity(
  380. 'mapper', 'mutate_param', array(
  381. 'name' => $name,
  382. 'attribute' => $attribute
  383. )
  384. );
  385. return false;
  386. }
  387. if ( ! isset( self::$sc[ $name ] ) ) {
  388. return trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $name ) );
  389. } elseif ( ! isset( $attribute['param_name'] ) ) {
  390. trigger_error( sprintf( __( "Wrong attribute for '%s' shortcode. Attribute 'param_name' required", "js_composer" ), $name ) );
  391. } else {
  392. $replaced = false;
  393. foreach ( self::$sc[ $name ]['params'] as $index => $param ) {
  394. if ( $param['param_name'] == $attribute['param_name'] ) {
  395. $replaced = true;
  396. self::$sc[ $name ]['params'][ $index ] = array_merge( $param, $attribute );
  397. }
  398. }
  399. if ( $replaced === false ) {
  400. self::$sc[ $name ]['params'][] = $attribute;
  401. }
  402. $sort = new Vc_Sort( self::$sc[ $name ]['params'] );
  403. self::$sc[ $name ]['params'] = $sort->sortByKey();
  404. visual_composer()->addShortCode( self::$sc[ $name ] );
  405. }
  406. return true;
  407. }
  408. /**
  409. * Removes shortcode from mapping list.
  410. *
  411. * @static
  412. *
  413. * @param $name
  414. *
  415. * @return bool
  416. */
  417. public static function dropShortcode( $name ) {
  418. if ( ! self::$is_init ) {
  419. vc_mapper()->addActivity(
  420. 'mapper', 'drop_shortcode', array(
  421. 'name' => $name
  422. )
  423. );
  424. return false;
  425. }
  426. unset( self::$sc[ $name ] );
  427. visual_composer()->removeShortCode( $name );
  428. return true;
  429. }
  430. /**
  431. * Modify shortcode's mapped settings.
  432. * You can modify only one option of the group options.
  433. * Call this method with $settings_name param as associated array to mass modifications.
  434. *
  435. * @static
  436. *
  437. * @param $name - shortcode' name.
  438. * @param $setting_name - option key name or the array of options.
  439. * @param $value - value of settings if $setting_name is option key.
  440. *
  441. * @return array|bool
  442. */
  443. public static function modify( $name, $setting_name, $value = '' ) {
  444. if ( ! self::$is_init ) {
  445. vc_mapper()->addActivity(
  446. 'mapper', 'modify', array(
  447. 'name' => $name,
  448. 'setting_name' => $setting_name,
  449. 'value' => $value
  450. )
  451. );
  452. return false;
  453. }
  454. if ( ! isset( self::$sc[ $name ] ) ) {
  455. return trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $name ) );
  456. } elseif ( $setting_name === 'base' ) {
  457. return trigger_error( sprintf( __( "Wrong setting_name for shortcode:%s. Base can't be modified.", "js_composer" ), $name ) );
  458. }
  459. if ( is_array( $setting_name ) ) {
  460. foreach ( $setting_name as $key => $value ) {
  461. self::modify( $name, $key, $value );
  462. }
  463. } else {
  464. self::$sc[ $name ][ $setting_name ] = $value;
  465. visual_composer()->updateShortcodeSetting( $name, $setting_name, $value );
  466. }
  467. return self::$sc;
  468. }
  469. /**
  470. * Returns "|" separated list of mapped shortcode tags.
  471. *
  472. * @static
  473. * @return string
  474. */
  475. public static function getTagsRegexp() {
  476. if ( empty( self::$tags_regexp ) ) {
  477. self::$tags_regexp = implode( '|', array_keys( self::$sc ) );
  478. }
  479. return self::$tags_regexp;
  480. }
  481. /**
  482. * Sorting method for WPBMap::generateUserData method. Called by usort php function.
  483. * @deprecated - use Vc_Sort::sortByKey since 4.4
  484. * @static
  485. *
  486. * @param $a
  487. * @param $b
  488. *
  489. * @return int
  490. */
  491. public static function sort( $a, $b ) {
  492. $a_weight = isset( $a['weight'] ) ? (int) $a['weight'] : 0;
  493. $b_weight = isset( $b['weight'] ) ? (int) $b['weight'] : 0;
  494. if ( $a_weight == $b_weight ) {
  495. $cmpa = array_search( $a, self::$user_sorted_sc );
  496. $cmpb = array_search( $b, self::$user_sorted_sc );
  497. return ( $cmpa > $cmpb ) ? 1 : - 1;
  498. }
  499. return ( $a_weight < $b_weight ) ? 1 : - 1;
  500. }
  501. }