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

https://gitlab.com/haque.mdmanzurul/barongbarong · PHP · 439 lines · 257 code · 29 blank · 153 comment · 53 complexity · f8f1665de2089d8ca469ae65a6926cd0 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. protected static $sc = Array();
  21. protected static $categories = Array();
  22. protected static $user_sc = false;
  23. protected static $user_sorted_sc = false;
  24. protected static $user_categories = false;
  25. protected static $settings, $user_role;
  26. protected static $tags_regexp;
  27. protected static $is_init = false;
  28. /**
  29. * Set init status fro WPMap.
  30. *
  31. * if $is_init is FALSE, then all activity like add, update and delete for shortcodes attributes will be hold in
  32. * the list of activity and will be executed after initialization.
  33. *
  34. * @see Vc_Mapper::iniy.
  35. * @static
  36. * @param bool $value
  37. */
  38. public static function setInit( $value = true ) {
  39. self::$is_init = $value;
  40. }
  41. /**
  42. * Gets user role and access rules for current user.
  43. *
  44. * @static
  45. * @return mixed
  46. */
  47. protected static function getSettings() {
  48. global $current_user;
  49. if ( self::$settings === null ) {
  50. if ( function_exists( 'get_currentuserinfo' ) ) {
  51. get_currentuserinfo();
  52. /** @var $settings - get use group access rules */
  53. if ( ! empty( $current_user->roles ) )
  54. self::$user_role = $current_user->roles[0];
  55. else
  56. self::$user_role = 'author';
  57. } else {
  58. self::$user_role = 'author';
  59. }
  60. self::$settings = vc_settings()->get( 'groups_access_rules' );
  61. }
  62. return self::$settings;
  63. }
  64. /**
  65. * Check is shortcode with a tag mapped to VC.
  66. *
  67. * @static
  68. * @param $tag - shortcode tag.
  69. * @return bool
  70. */
  71. public static function exists( $tag ) {
  72. return (boolean)isset( self::$sc[$tag] );
  73. }
  74. /**
  75. * Map shortcode to VC.
  76. *
  77. * This method maps shortcode to VC.
  78. * You need to shortcode's tag and settings to map correctly.
  79. * Default shortcodes are mapped in config/map.php file.
  80. * The best way is to call this method with "init" action callback function of WP.
  81. *
  82. * @static
  83. * @param $tag
  84. * @param $attributes
  85. * @return bool
  86. */
  87. public static function map( $tag, $attributes ) {
  88. if ( ! self::$is_init ) {
  89. vc_mapper()->addActivity(
  90. 'mapper', 'map', array(
  91. 'tag' => $tag,
  92. 'attributes' => $attributes
  93. )
  94. );
  95. return false;
  96. }
  97. if ( empty( $attributes['name'] ) ) {
  98. trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $tag ) );
  99. } elseif ( empty( $attributes['base'] ) ) {
  100. trigger_error( sprintf( __( "Wrong base for shortcode:%s. Base required", "js_composer" ), $tag ) );
  101. } else {
  102. self::$sc[$tag] = $attributes;
  103. self::$sc[$tag]['params'] = Array();
  104. if ( ! empty( $attributes['params'] ) ) {
  105. foreach ( $attributes['params'] as $attribute ) {
  106. $attribute = apply_filters('vc_mapper_attribute', $attribute, $tag);
  107. $attribute = apply_filters('vc_mapper_attribute_'.$attribute['type'], $attribute, $tag);
  108. self::$sc[$tag]['params'][] = $attribute;
  109. }
  110. }
  111. visual_composer()->addShortCode( self::$sc[$tag] );
  112. }
  113. }
  114. /**
  115. * Generates list of shortcodes taking into account the access rules for shortcodes from VC Settings page.
  116. *
  117. * This method parses the list of mapped shortcodes and creates categories list for users.
  118. *
  119. * @static
  120. * @param bool $force - force data generation even data already generated.
  121. */
  122. protected static function generateUserData( $force = false ) {
  123. if ( ! $force && self::$user_sc !== false && self::$user_categories !== false ) return;
  124. $settings = self::getSettings();
  125. self::$user_sc = self::$user_categories = self::$user_sorted_sc = array();
  126. foreach ( self::$sc as $name => $values ) {
  127. if ( ! isset( $settings[self::$user_role]['shortcodes'] )
  128. || ( isset( $settings[self::$user_role]['shortcodes'][$name] ) && (int)$settings[self::$user_role]['shortcodes'][$name] == 1 )
  129. ) {
  130. if ( $name != 'vc_column' && ( ! isset( $values['content_element'] ) || $values['content_element'] === true ) ) {
  131. $categories = isset( $values['category'] ) ? $values['category'] : '_other_category_';
  132. $values['_category_ids'] = array();
  133. if ( is_array( $categories ) ) {
  134. foreach ( $categories as $c ) {
  135. if ( array_search( $c, self::$user_categories ) === false ) self::$user_categories[] = $c;
  136. $values['_category_ids'][] = md5( $c ); // array_search($category, self::$categories);
  137. }
  138. } else {
  139. if ( array_search( $categories, self::$user_categories ) === false ) self::$user_categories[] = $categories;
  140. $values['_category_ids'][] = md5( $categories ); // array_search($category, self::$categories);
  141. }
  142. }
  143. self::$user_sc[$name] = $values;
  144. self::$user_sorted_sc[] = $values;
  145. }
  146. }
  147. @usort( self::$user_sorted_sc, array( "WPBMap", "sort" ) );
  148. }
  149. /**
  150. * Get mapped shortcode settings.
  151. *
  152. * @static
  153. * @return array
  154. */
  155. public static function getShortCodes() {
  156. return self::$sc;
  157. }
  158. /**
  159. * Get sorted list of mapped shortcode settings for current user.
  160. *
  161. * Sorting depends on the weight attribute and mapping order.
  162. *
  163. * @static
  164. * @return array
  165. */
  166. public static function getSortedUserShortCodes() {
  167. self::generateUserData();
  168. return self::$user_sorted_sc;
  169. }
  170. /**
  171. * Get list of mapped shortcode settings for current user.
  172. * @static
  173. * @return array - associated array of shortcodes settings with tag as the key.
  174. */
  175. public static function getUserShortCodes() {
  176. self::generateUserData();
  177. return self::$user_sc;
  178. }
  179. /**
  180. * Get mapped shortcode settings by tag.
  181. *
  182. * @static
  183. * @param $tag - shortcode tag.
  184. * @return array
  185. */
  186. public static function getShortCode( $tag ) {
  187. return self::$sc[$tag];
  188. }
  189. /**
  190. * Get all categories for mapped shortcodes.
  191. *
  192. * @static
  193. * @return array
  194. */
  195. public static function getCategories() {
  196. return self::$categories;
  197. }
  198. /**
  199. * Get all categories for current user.
  200. *
  201. * Category is added to the list when at least one shortcode of this category is allowed for current user
  202. * by Vc access rules.
  203. *
  204. * @static
  205. * @return bool
  206. */
  207. public static function getUserCategories() {
  208. self::generateUserData();
  209. return self::$user_categories;
  210. }
  211. /**
  212. * Drop shortcode param.
  213. *
  214. * @static
  215. * @param $name
  216. * @param $attribute_name
  217. */
  218. public static function dropParam( $name, $attribute_name ) {
  219. if ( ! self::$is_init ) {
  220. vc_mapper()->addActivity(
  221. 'mapper', 'drop_param', array(
  222. 'name' => $name,
  223. 'attribute_name' => $attribute_name
  224. )
  225. );
  226. return;
  227. }
  228. foreach ( self::$sc[$name]['params'] as $index => $param ) {
  229. if ( $param['param_name'] == $attribute_name ) {
  230. array_splice( self::$sc[$name]['params'], $index, 1 );
  231. return;
  232. }
  233. }
  234. }
  235. /**
  236. * Returns param settings for mapped shortcodes.
  237. *
  238. * @static
  239. * @param $tag
  240. * @param $param_name
  241. *
  242. * @return bool| array
  243. */
  244. public static function getParam( $tag, $param_name ) {
  245. if ( ! isset( self::$sc[$tag] ) )
  246. return trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $tag ) );
  247. foreach ( self::$sc[$tag]['params'] as $index => $param ) {
  248. if ( $param['param_name'] == $param_name ) {
  249. return self::$sc[$tag]['params'][$index];
  250. }
  251. }
  252. return false;
  253. }
  254. /**
  255. * Add new param to shortcode params list.
  256. *
  257. * @static
  258. * @param $name
  259. * @param array $attribute
  260. * @return bool
  261. */
  262. public static function addParam( $name, $attribute = Array() ) {
  263. if ( ! self::$is_init ) {
  264. vc_mapper()->addActivity(
  265. 'mapper', 'add_param', array(
  266. 'name' => $name,
  267. 'attribute' => $attribute
  268. )
  269. );
  270. return;
  271. }
  272. if ( ! isset( self::$sc[$name] ) )
  273. return trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $name ) );
  274. elseif ( ! isset( $attribute['param_name'] ) ) {
  275. trigger_error( sprintf( __( "Wrong attribute for '%s' shortcode. Attribute 'param_name' required", "js_composer" ), $name ) );
  276. } else {
  277. $replaced = false;
  278. foreach ( self::$sc[$name]['params'] as $index => $param ) {
  279. if ( $param['param_name'] == $attribute['param_name'] ) {
  280. $replaced = true;
  281. self::$sc[$name]['params'][$index] = $attribute;
  282. }
  283. }
  284. if ( $replaced === false ) self::$sc[$name]['params'][] = $attribute;
  285. visual_composer()->addShortCode( self::$sc[$name] );
  286. }
  287. }
  288. /**
  289. * Change param attributes of mapped shortcode.
  290. *
  291. * @static
  292. * @param $name
  293. * @param array $attribute
  294. * @return bool
  295. */
  296. public static function mutateParam( $name, $attribute = Array() ) {
  297. if ( ! self::$is_init ) {
  298. vc_mapper()->addActivity(
  299. 'mapper', 'mutate_param', array(
  300. 'name' => $name,
  301. 'attribute' => $attribute
  302. )
  303. );
  304. return false;
  305. }
  306. if ( ! isset( self::$sc[$name] ) )
  307. return trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $name ) );
  308. elseif ( ! isset( $attribute['param_name'] ) ) {
  309. trigger_error( sprintf( __( "Wrong attribute for '%s' shortcode. Attribute 'param_name' required", "js_composer" ), $name ) );
  310. } else {
  311. $replaced = false;
  312. foreach ( self::$sc[$name]['params'] as $index => $param ) {
  313. if ( $param['param_name'] == $attribute['param_name'] ) {
  314. $replaced = true;
  315. self::$sc[$name]['params'][$index] = array_merge( $param, $attribute );
  316. }
  317. }
  318. if ( $replaced === false ) self::$sc[$name]['params'][] = $attribute;
  319. visual_composer()->addShortCode( self::$sc[$name] );
  320. }
  321. return true;
  322. }
  323. /**
  324. * Removes shortcode from mapping list.
  325. *
  326. * @static
  327. * @param $name
  328. * @return bool
  329. */
  330. public static function dropShortcode( $name ) {
  331. if ( ! self::$is_init ) {
  332. vc_mapper()->addActivity(
  333. 'mapper', 'drop_shortcode', array(
  334. 'name' => $name
  335. )
  336. );
  337. return false;
  338. }
  339. unset( self::$sc[$name] );
  340. visual_composer()->removeShortCode( $name );
  341. }
  342. /**
  343. * Modify shortcode's mapped settings.
  344. * You can modify only one option of the group options.
  345. * Call this method with $settings_name param as associated array to mass modifications.
  346. *
  347. * @static
  348. * @param $name - shortcode' name.
  349. * @param $setting_name - option key name or the array of options.
  350. * @param $value - value of settings if $setting_name is option key.
  351. * @return array|bool
  352. */
  353. public static function modify( $name, $setting_name, $value = '' ) {
  354. if ( ! self::$is_init ) {
  355. vc_mapper()->addActivity(
  356. 'mapper', 'modify', array(
  357. 'name' => $name,
  358. 'setting_name' => $setting_name,
  359. 'value' => $value
  360. )
  361. );
  362. return false;
  363. }
  364. if ( ! isset( self::$sc[$name] ) )
  365. return trigger_error( sprintf( __( "Wrong name for shortcode:%s. Name required", "js_composer" ), $name ) );
  366. elseif ( $setting_name === 'base' ) {
  367. return trigger_error( sprintf( __( "Wrong setting_name for shortcode:%s. Base can't be modified.", "js_composer" ), $name ) );
  368. }
  369. if ( is_array( $setting_name ) ) {
  370. foreach ( $setting_name as $key => $value ) {
  371. self::modify( $name, $key, $value );
  372. }
  373. } else {
  374. self::$sc[$name][$setting_name] = $value;
  375. }
  376. return self::$sc;
  377. }
  378. /**
  379. * Returns "|" separated list of mapped shortcode tags.
  380. *
  381. * @static
  382. * @return string
  383. */
  384. public static function getTagsRegexp() {
  385. if ( empty( self::$tags_regexp ) ) {
  386. self::$tags_regexp = implode( '|', array_keys( self::$sc ) );
  387. }
  388. return self::$tags_regexp;
  389. }
  390. /**
  391. * Sorting method for WPBMap::generateUserData method. Called by usort php function.
  392. *
  393. * @static
  394. * @param $a
  395. * @param $b
  396. * @return int
  397. */
  398. public static function sort( $a, $b ) {
  399. $a_weight = isset( $a['weight'] ) ? (int)$a['weight'] : 0;
  400. $b_weight = isset( $b['weight'] ) ? (int)$b['weight'] : 0;
  401. if ( $a_weight == $b_weight ) {
  402. $cmpa = array_search( $a, self::$user_sorted_sc );
  403. $cmpb = array_search( $b, self::$user_sorted_sc );
  404. return ( $cmpa > $cmpb ) ? 1 : - 1;
  405. }
  406. return ( $a_weight < $b_weight ) ? 1 : - 1;
  407. }
  408. }