helpers_api.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * @param $attributes
  7. * @return bool
  8. * @throws \Exception
  9. */
  10. function wpb_map( $attributes ) {
  11. return vc_map( $attributes );
  12. }
  13. /**
  14. * Lean map shortcodes
  15. *
  16. * @param $tag
  17. * @param null $settings_function
  18. * @param null $settings_file
  19. * @since 4.9
  20. *
  21. */
  22. function vc_lean_map( $tag, $settings_function = null, $settings_file = null ) {
  23. WPBMap::leanMap( $tag, $settings_function, $settings_file );
  24. }
  25. /**
  26. * @param $attributes
  27. *
  28. * @return bool
  29. * @throws \Exception
  30. * @since 4.2
  31. */
  32. function vc_map( $attributes ) {
  33. if ( ! isset( $attributes['base'] ) ) {
  34. throw new Exception( esc_html__( 'Wrong vc_map object. Base attribute is required', 'js_composer' ) );
  35. }
  36. return WPBMap::map( $attributes['base'], $attributes );
  37. }
  38. /**
  39. * @param $shortcode
  40. *
  41. * @since 4.2
  42. */
  43. function vc_remove_element( $shortcode ) {
  44. WPBMap::dropShortcode( $shortcode );
  45. }
  46. /**
  47. * Add new shortcode param.
  48. *
  49. * @param $shortcode - tag for shortcode
  50. * @param $attributes - attribute settings
  51. * @throws \Exception
  52. * @since 4.2
  53. *
  54. */
  55. function vc_add_param( $shortcode, $attributes ) {
  56. WPBMap::addParam( $shortcode, $attributes );
  57. }
  58. /**
  59. * Mass shortcode params adding function
  60. *
  61. * @param $shortcode - tag for shortcode
  62. * @param $attributes - list of attributes arrays
  63. * @throws \Exception
  64. * @since 4.3
  65. *
  66. */
  67. function vc_add_params( $shortcode, $attributes ) {
  68. if ( is_array( $attributes ) ) {
  69. foreach ( $attributes as $attr ) {
  70. vc_add_param( $shortcode, $attr );
  71. }
  72. }
  73. }
  74. /**
  75. * Shorthand function for WPBMap::modify
  76. *
  77. * @param string $name
  78. * @param string $setting
  79. * @param string $value
  80. *
  81. * @return array|bool
  82. * @throws \Exception
  83. * @since 4.2
  84. */
  85. function vc_map_update( $name = '', $setting = '', $value = '' ) {
  86. return WPBMap::modify( $name, $setting, $value );
  87. }
  88. /**
  89. * Shorthand function for WPBMap::mutateParam
  90. *
  91. * @param $name
  92. * @param array $attribute
  93. *
  94. * @return bool
  95. * @throws \Exception
  96. * @since 4.2
  97. */
  98. function vc_update_shortcode_param( $name, $attribute = array() ) {
  99. return WPBMap::mutateParam( $name, $attribute );
  100. }
  101. /**
  102. * Shorthand function for WPBMap::dropParam
  103. *
  104. * @param $name
  105. * @param $attribute_name
  106. *
  107. * @return bool
  108. * @since 4.2
  109. */
  110. function vc_remove_param( $name = '', $attribute_name = '' ) {
  111. return WPBMap::dropParam( $name, $attribute_name );
  112. }
  113. if ( ! function_exists( 'vc_set_as_theme' ) ) {
  114. /**
  115. * Sets plugin as theme plugin.
  116. *
  117. * @internal param bool $disable_updater - If value is true disables auto updater options.
  118. *
  119. * @since 4.2
  120. */
  121. function vc_set_as_theme() {
  122. vc_manager()->setIsAsTheme( true );
  123. }
  124. }
  125. if ( ! function_exists( 'vc_is_as_theme' ) ) {
  126. /**
  127. * Is VC as-theme-plugin.
  128. * @return bool
  129. * @since 4.2
  130. */
  131. function vc_is_as_theme() {
  132. return vc_manager()->isAsTheme();
  133. }
  134. }
  135. if ( ! function_exists( 'vc_is_updater_disabled' ) ) {
  136. /**
  137. * @return bool
  138. * @since 4.2
  139. */
  140. function vc_is_updater_disabled() {
  141. return vc_manager()->isUpdaterDisabled();
  142. }
  143. }
  144. if ( ! function_exists( 'vc_default_editor_post_types' ) ) {
  145. /**
  146. * Returns list of default post type.
  147. * @return array
  148. * @since 4.2
  149. */
  150. function vc_default_editor_post_types() {
  151. return vc_manager()->editorDefaultPostTypes();
  152. }
  153. }
  154. if ( ! function_exists( 'vc_set_default_editor_post_types' ) ) {
  155. /**
  156. * Set post types for VC editor.
  157. * @param array $list - list of valid post types to set
  158. * @since 4.2
  159. *
  160. */
  161. function vc_set_default_editor_post_types( array $list ) {
  162. vc_manager()->setEditorDefaultPostTypes( $list );
  163. }
  164. }
  165. if ( ! function_exists( ( 'vc_editor_post_types' ) ) ) {
  166. /**
  167. * Returns list of post types where VC editor is enabled.
  168. * @return array
  169. * @since 4.2
  170. */
  171. function vc_editor_post_types() {
  172. return vc_manager()->editorPostTypes();
  173. }
  174. }
  175. if ( ! function_exists( ( 'vc_editor_set_post_types' ) ) ) {
  176. /**
  177. * Set list of post types where VC editor is enabled.
  178. * @param array $post_types
  179. * @throws \Exception
  180. * @since 4.4
  181. *
  182. */
  183. function vc_editor_set_post_types( array $post_types ) {
  184. vc_manager()->setEditorPostTypes( $post_types );
  185. }
  186. }
  187. if ( ! function_exists( 'vc_mode' ) ) {
  188. /**
  189. * Return current VC mode.
  190. * @return string
  191. * @see Vc_Mapper::$mode
  192. * @since 4.2
  193. */
  194. function vc_mode() {
  195. return vc_manager()->mode();
  196. }
  197. }
  198. if ( ! function_exists( 'vc_set_shortcodes_templates_dir' ) ) {
  199. /**
  200. * Sets directory where WPBakery Page Builder should look for template files for content elements.
  201. * @param string - full directory path to new template directory with trailing slash
  202. * @since 4.2
  203. *
  204. */
  205. function vc_set_shortcodes_templates_dir( $dir ) {
  206. vc_manager()->setCustomUserShortcodesTemplateDir( $dir );
  207. }
  208. }
  209. if ( ! function_exists( 'vc_shortcodes_theme_templates_dir' ) ) {
  210. /**
  211. * Get custom theme template path
  212. * @param $template - filename for template
  213. *
  214. * @return string
  215. * @since 4.2
  216. *
  217. */
  218. function vc_shortcodes_theme_templates_dir( $template ) {
  219. return vc_manager()->getShortcodesTemplateDir( $template );
  220. }
  221. }
  222. /**
  223. * @param bool $value
  224. *
  225. * @todo check usage.
  226. *
  227. * @since 4.3
  228. */
  229. function set_vc_is_inline( $value = true ) {
  230. _deprecated_function( 'set_vc_is_inline', '5.2 (will be removed in 5.3)' );
  231. global $vc_is_inline;
  232. $vc_is_inline = $value;
  233. }
  234. /**
  235. * Disable frontend editor for VC
  236. * @param bool $disable
  237. * @since 4.3
  238. *
  239. */
  240. function vc_disable_frontend( $disable = true ) {
  241. vc_frontend_editor()->disableInline( $disable );
  242. }
  243. /**
  244. * Check is front end enabled.
  245. * @return bool
  246. * @throws \Exception
  247. * @since 4.3
  248. */
  249. function vc_enabled_frontend() {
  250. return vc_frontend_editor()->frontendEditorEnabled();
  251. }
  252. if ( ! function_exists( 'vc_add_default_templates' ) ) {
  253. /**
  254. * Add custom template in default templates list
  255. *
  256. * @param array $data | template data (name, content, custom_class, image_path)
  257. *
  258. * @return bool
  259. * @since 4.3
  260. */
  261. function vc_add_default_templates( $data ) {
  262. return visual_composer()->templatesPanelEditor()->addDefaultTemplates( $data );
  263. }
  264. }
  265. /**
  266. * @param $shortcode
  267. * @param string $field_prefix
  268. * @param string $group_prefix
  269. * @param null $change_fields
  270. * @param null $dependency
  271. * @return array
  272. * @throws \Exception
  273. */
  274. function vc_map_integrate_shortcode( $shortcode, $field_prefix = '', $group_prefix = '', $change_fields = null, $dependency = null ) {
  275. if ( is_string( $shortcode ) ) {
  276. $shortcode_data = WPBMap::getShortCode( $shortcode );
  277. } else {
  278. $shortcode_data = $shortcode;
  279. }
  280. if ( is_array( $shortcode_data ) && ! empty( $shortcode_data ) ) {
  281. /**
  282. * @var WPBakeryShortCodeFishBones $shortcode
  283. */
  284. $params = isset( $shortcode_data['params'] ) && ! empty( $shortcode_data['params'] ) ? $shortcode_data['params'] : false;
  285. if ( is_array( $params ) && ! empty( $params ) ) {
  286. $keys = array_keys( $params );
  287. $count = count( $keys );
  288. for ( $i = 0; $i < $count; $i ++ ) {
  289. $param = &$params[ $keys[ $i ] ]; // Note! passed by reference to automatically update data
  290. if ( isset( $change_fields ) ) {
  291. $param = vc_map_integrate_include_exclude_fields( $param, $change_fields );
  292. if ( empty( $param ) ) {
  293. continue;
  294. }
  295. }
  296. if ( ! empty( $group_prefix ) ) {
  297. if ( isset( $param['group'] ) ) {
  298. $param['group'] = $group_prefix . ': ' . $param['group'];
  299. } else {
  300. $param['group'] = $group_prefix;
  301. }
  302. }
  303. if ( ! empty( $field_prefix ) && isset( $param['param_name'] ) ) {
  304. $param['param_name'] = $field_prefix . $param['param_name'];
  305. if ( isset( $param['dependency'] ) && is_array( $param['dependency'] ) && isset( $param['dependency']['element'] ) ) {
  306. $param['dependency']['element'] = $field_prefix . $param['dependency']['element'];
  307. }
  308. $param = vc_map_integrate_add_dependency( $param, $dependency );
  309. } elseif ( ! empty( $dependency ) ) {
  310. $param = vc_map_integrate_add_dependency( $param, $dependency );
  311. }
  312. $param['integrated_shortcode'] = is_array( $shortcode ) ? $shortcode['base'] : $shortcode;
  313. $param['integrated_shortcode_field'] = $field_prefix;
  314. }
  315. }
  316. return is_array( $params ) ? array_filter( $params ) : array();
  317. }
  318. return array();
  319. }
  320. /**
  321. * Used to filter params (include/exclude)
  322. *
  323. * @param $param
  324. * @param $change_fields
  325. *
  326. * @return array|null
  327. * @internal
  328. *
  329. */
  330. function vc_map_integrate_include_exclude_fields( $param, $change_fields ) {
  331. if ( is_array( $change_fields ) ) {
  332. if ( isset( $change_fields['exclude'] ) && in_array( $param['param_name'], $change_fields['exclude'], true ) ) {
  333. $param = null;
  334. return $param; // to prevent group adding to $param
  335. } elseif ( isset( $change_fields['exclude_regex'] ) ) {
  336. if ( is_array( $change_fields['exclude_regex'] ) && ! empty( $change_fields['exclude_regex'] ) ) {
  337. $break_foreach = false;
  338. foreach ( $change_fields['exclude_regex'] as $regex ) {
  339. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  340. // @codingStandardsIgnoreLine
  341. if ( @preg_match( $regex, null ) ) {
  342. if ( preg_match( $regex, $param['param_name'] ) ) {
  343. $param = null;
  344. $break_foreach = true;
  345. }
  346. }
  347. if ( $break_foreach ) {
  348. break;
  349. }
  350. }
  351. if ( $break_foreach ) {
  352. return $param; // to prevent group adding to $param
  353. }
  354. } elseif ( is_string( $change_fields['exclude_regex'] ) && strlen( $change_fields['exclude_regex'] ) > 0 ) {
  355. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  356. // @codingStandardsIgnoreLine
  357. if ( @preg_match( $change_fields['exclude_regex'], null ) ) {
  358. if ( preg_match( $change_fields['exclude_regex'], $param['param_name'] ) ) {
  359. $param = null;
  360. return $param; // to prevent group adding to $param
  361. }
  362. }
  363. }
  364. }
  365. if ( isset( $change_fields['include_only'] ) && ! in_array( $param['param_name'], $change_fields['include_only'], true ) ) {
  366. // if we want to enclude only some fields
  367. $param = null;
  368. return $param; // to prevent group adding to $param
  369. } elseif ( isset( $change_fields['include_only_regex'] ) ) {
  370. if ( is_array( $change_fields['include_only_regex'] ) && ! empty( $change_fields['include_only_regex'] ) ) {
  371. $break_foreach = false;
  372. foreach ( $change_fields['include_only_regex'] as $regex ) {
  373. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  374. // @codingStandardsIgnoreLine
  375. if ( false === @preg_match( $regex, null ) ) {
  376. // Regular expression is invalid, (don't remove @).
  377. } else {
  378. if ( ! preg_match( $regex, $param['param_name'] ) ) {
  379. $param = null;
  380. $break_foreach = true;
  381. }
  382. }
  383. if ( $break_foreach ) {
  384. break;
  385. }
  386. }
  387. if ( $break_foreach ) {
  388. return $param; // to prevent group adding to $param
  389. }
  390. } elseif ( is_string( $change_fields['include_only_regex'] ) && strlen( $change_fields['include_only_regex'] ) > 0 ) {
  391. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  392. // @codingStandardsIgnoreLine
  393. if ( false === @preg_match( $change_fields['include_only_regex'], null ) ) {
  394. // Regular expression is invalid, (don't remove @).
  395. } else {
  396. if ( ! preg_match( $change_fields['include_only_regex'], $param['param_name'] ) ) {
  397. $param = null;
  398. return $param; // to prevent group adding to $param
  399. }
  400. }
  401. }
  402. }
  403. }
  404. return $param;
  405. }
  406. /**
  407. * @param $param
  408. * @param $dependency
  409. *
  410. * @return array
  411. * @internal used to add dependency to existed param
  412. *
  413. */
  414. function vc_map_integrate_add_dependency( $param, $dependency ) {
  415. // activator must be used for all elements who doesn't have 'dependency'
  416. if ( ! empty( $dependency ) && ( ! isset( $param['dependency'] ) || empty( $param['dependency'] ) ) ) {
  417. if ( is_array( $dependency ) ) {
  418. $param['dependency'] = $dependency;
  419. }
  420. }
  421. return $param;
  422. }
  423. /**
  424. * @param $base_shortcode
  425. * @param $integrated_shortcode
  426. * @param string $field_prefix
  427. * @return array
  428. * @throws \Exception
  429. */
  430. function vc_map_integrate_get_params( $base_shortcode, $integrated_shortcode, $field_prefix = '' ) {
  431. $shortcode_data = WPBMap::getShortCode( $base_shortcode );
  432. $params = array();
  433. if ( is_array( $shortcode_data ) && is_array( $shortcode_data['params'] ) && ! empty( $shortcode_data['params'] ) ) {
  434. foreach ( $shortcode_data['params'] as $param ) {
  435. if ( is_array( $param ) && isset( $param['integrated_shortcode'] ) && $integrated_shortcode === $param['integrated_shortcode'] ) {
  436. if ( ! empty( $field_prefix ) ) {
  437. if ( isset( $param['integrated_shortcode_field'] ) && $field_prefix === $param['integrated_shortcode_field'] ) {
  438. $params[] = $param;
  439. }
  440. } else {
  441. $params[] = $param;
  442. }
  443. }
  444. }
  445. }
  446. return $params;
  447. }
  448. /**
  449. * @param $base_shortcode
  450. * @param $integrated_shortcode
  451. * @param string $field_prefix
  452. * @return array
  453. * @throws \Exception
  454. */
  455. function vc_map_integrate_get_atts( $base_shortcode, $integrated_shortcode, $field_prefix = '' ) {
  456. $params = vc_map_integrate_get_params( $base_shortcode, $integrated_shortcode, $field_prefix );
  457. $atts = array();
  458. if ( is_array( $params ) && ! empty( $params ) ) {
  459. foreach ( $params as $param ) {
  460. $value = '';
  461. if ( isset( $param['value'] ) ) {
  462. if ( isset( $param['std'] ) ) {
  463. $value = $param['std'];
  464. } elseif ( is_array( $param['value'] ) ) {
  465. reset( $param['value'] );
  466. $value = current( $param['value'] );
  467. } else {
  468. $value = $param['value'];
  469. }
  470. }
  471. $atts[ $param['param_name'] ] = $value;
  472. }
  473. }
  474. return $atts;
  475. }
  476. /**
  477. * @param $base_shortcode
  478. * @param $integrated_shortcode
  479. * @param $atts
  480. * @param string $field_prefix
  481. * @return array
  482. * @throws \Exception
  483. */
  484. function vc_map_integrate_parse_atts( $base_shortcode, $integrated_shortcode, $atts, $field_prefix = '' ) {
  485. $params = vc_map_integrate_get_params( $base_shortcode, $integrated_shortcode, $field_prefix );
  486. $data = array();
  487. if ( is_array( $params ) && ! empty( $params ) ) {
  488. foreach ( $params as $param ) {
  489. if ( isset( $atts[ $param['param_name'] ] ) ) {
  490. $value = $atts[ $param['param_name'] ];
  491. }
  492. if ( isset( $value ) ) {
  493. $key = $param['param_name'];
  494. if ( strlen( $field_prefix ) > 0 ) {
  495. $key = substr( $key, strlen( $field_prefix ) );
  496. }
  497. $data[ $key ] = $value;
  498. }
  499. }
  500. }
  501. return $data;
  502. }
  503. /**
  504. * @param bool $label
  505. * @return mixed|void
  506. */
  507. function vc_map_add_css_animation( $label = true ) {
  508. $data = array(
  509. 'type' => 'animation_style',
  510. 'heading' => esc_html__( 'CSS Animation', 'js_composer' ),
  511. 'param_name' => 'css_animation',
  512. 'admin_label' => $label,
  513. 'value' => '',
  514. 'settings' => array(
  515. 'type' => 'in',
  516. 'custom' => array(
  517. array(
  518. 'label' => esc_html__( 'Default', 'js_composer' ),
  519. 'values' => array(
  520. esc_html__( 'Top to bottom', 'js_composer' ) => 'top-to-bottom',
  521. esc_html__( 'Bottom to top', 'js_composer' ) => 'bottom-to-top',
  522. esc_html__( 'Left to right', 'js_composer' ) => 'left-to-right',
  523. esc_html__( 'Right to left', 'js_composer' ) => 'right-to-left',
  524. esc_html__( 'Appear from center', 'js_composer' ) => 'appear',
  525. ),
  526. ),
  527. ),
  528. ),
  529. 'description' => esc_html__( 'Select type of animation for element to be animated when it "enters" the browsers viewport (Note: works only in modern browsers).', 'js_composer' ),
  530. );
  531. return apply_filters( 'vc_map_add_css_animation', $data, $label );
  532. }
  533. /**
  534. * Get settings of the mapped shortcode.
  535. *
  536. * @param $tag
  537. *
  538. * @return array|null - settings or null if shortcode not mapped
  539. * @throws \Exception
  540. * @since 4.4.3
  541. */
  542. function vc_get_shortcode( $tag ) {
  543. return WPBMap::getShortCode( $tag );
  544. }
  545. /**
  546. * Remove all mapped shortcodes and the moment when function is called.
  547. *
  548. * @since 4.5
  549. */
  550. function vc_remove_all_elements() {
  551. WPBMap::dropAllShortcodes();
  552. }
  553. /**
  554. * Function to get defaults values for shortcode.
  555. * @param $tag - shortcode tag
  556. *
  557. * @return array - list of param=>default_value
  558. * @throws \Exception
  559. * @since 4.6
  560. *
  561. */
  562. function vc_map_get_defaults( $tag ) {
  563. $shortcode = vc_get_shortcode( $tag );
  564. $params = array();
  565. if ( is_array( $shortcode ) && isset( $shortcode['params'] ) && ! empty( $shortcode['params'] ) ) {
  566. $params = vc_map_get_params_defaults( $shortcode['params'] );
  567. }
  568. return $params;
  569. }
  570. /**
  571. * @param $params
  572. *
  573. * @return array
  574. * @since 4.12
  575. */
  576. function vc_map_get_params_defaults( $params ) {
  577. $resultParams = array();
  578. foreach ( $params as $param ) {
  579. if ( isset( $param['param_name'] ) && 'content' !== $param['param_name'] ) {
  580. $value = '';
  581. if ( isset( $param['std'] ) ) {
  582. $value = $param['std'];
  583. } elseif ( isset( $param['value'] ) ) {
  584. if ( is_array( $param['value'] ) ) {
  585. $value = current( $param['value'] );
  586. if ( is_array( $value ) ) {
  587. // in case if two-dimensional array provided (vc_basic_grid)
  588. $value = current( $value );
  589. }
  590. // return first value from array (by default)
  591. } else {
  592. $value = $param['value'];
  593. }
  594. }
  595. $resultParams[ $param['param_name'] ] = apply_filters( 'vc_map_get_param_defaults', $value, $param );
  596. }
  597. }
  598. return $resultParams;
  599. }
  600. /**
  601. * @param $tag - shortcode tag3
  602. * @param array $atts - shortcode attributes
  603. *
  604. * @return array - return merged values with provided attributes (
  605. * 'a'=>1,'b'=>2 + 'b'=>3,'c'=>4 --> 'a'=>1,'b'=>3 )
  606. *
  607. * @throws \Exception
  608. * @see vc_shortcode_attribute_parse - return union of provided attributes (
  609. * 'a'=>1,'b'=>2 + 'b'=>3,'c'=>4 --> 'a'=>1,
  610. * 'b'=>3, 'c'=>4 )
  611. */
  612. function vc_map_get_attributes( $tag, $atts = array() ) {
  613. $atts = shortcode_atts( vc_map_get_defaults( $tag ), $atts, $tag );
  614. return apply_filters( 'vc_map_get_attributes', $atts, $tag );
  615. }
  616. /**
  617. * @param $name
  618. * @return mixed|string
  619. */
  620. function vc_convert_vc_color( $name ) {
  621. $colors = array(
  622. 'blue' => '#5472d2',
  623. 'turquoise' => '#00c1cf',
  624. 'pink' => '#fe6c61',
  625. 'violet' => '#8d6dc4',
  626. 'peacoc' => '#4cadc9',
  627. 'chino' => '#cec2ab',
  628. 'mulled-wine' => '#50485b',
  629. 'vista-blue' => '#75d69c',
  630. 'orange' => '#f7be68',
  631. 'sky' => '#5aa1e3',
  632. 'green' => '#6dab3c',
  633. 'juicy-pink' => '#f4524d',
  634. 'sandy-brown' => '#f79468',
  635. 'purple' => '#b97ebb',
  636. 'black' => '#2a2a2a',
  637. 'grey' => '#ebebeb',
  638. 'white' => '#ffffff',
  639. );
  640. $name = str_replace( '_', '-', $name );
  641. if ( isset( $colors[ $name ] ) ) {
  642. return $colors[ $name ];
  643. }
  644. return '';
  645. }
  646. /**
  647. * Extract width/height from string
  648. *
  649. * @param string $dimensions WxH
  650. *
  651. * @return mixed array(width, height) or false
  652. * @since 4.7
  653. *
  654. */
  655. function vc_extract_dimensions( $dimensions ) {
  656. $dimensions = str_replace( ' ', '', $dimensions );
  657. $matches = null;
  658. if ( preg_match( '/(\d+)x(\d+)/', $dimensions, $matches ) ) {
  659. return array(
  660. $matches[1],
  661. $matches[2],
  662. );
  663. }
  664. return false;
  665. }
  666. /**
  667. * @param string $asset
  668. *
  669. * @return array|string
  670. */
  671. function vc_get_shared( $asset = '' ) {
  672. switch ( $asset ) {
  673. case 'colors':
  674. $asset = VcSharedLibrary::getColors();
  675. break;
  676. case 'colors-dashed':
  677. $asset = VcSharedLibrary::getColorsDashed();
  678. break;
  679. case 'icons':
  680. $asset = VcSharedLibrary::getIcons();
  681. break;
  682. case 'sizes':
  683. $asset = VcSharedLibrary::getSizes();
  684. break;
  685. case 'button styles':
  686. case 'alert styles':
  687. $asset = VcSharedLibrary::getButtonStyles();
  688. break;
  689. case 'message_box_styles':
  690. $asset = VcSharedLibrary::getMessageBoxStyles();
  691. break;
  692. case 'cta styles':
  693. $asset = VcSharedLibrary::getCtaStyles();
  694. break;
  695. case 'text align':
  696. $asset = VcSharedLibrary::getTextAlign();
  697. break;
  698. case 'cta widths':
  699. case 'separator widths':
  700. $asset = VcSharedLibrary::getElementWidths();
  701. break;
  702. case 'separator styles':
  703. $asset = VcSharedLibrary::getSeparatorStyles();
  704. break;
  705. case 'separator border widths':
  706. $asset = VcSharedLibrary::getBorderWidths();
  707. break;
  708. case 'single image styles':
  709. $asset = VcSharedLibrary::getBoxStyles();
  710. break;
  711. case 'single image external styles':
  712. $asset = VcSharedLibrary::getBoxStyles( array(
  713. 'default',
  714. 'round',
  715. ) );
  716. break;
  717. case 'toggle styles':
  718. $asset = VcSharedLibrary::getToggleStyles();
  719. break;
  720. case 'animation styles':
  721. $asset = VcSharedLibrary::getAnimationStyles();
  722. break;
  723. }
  724. return $asset;
  725. }
  726. /**
  727. * Helper function to register new shortcode attribute hook.
  728. *
  729. * @param $name - attribute name
  730. * @param $form_field_callback - hook, will be called when settings form is shown and attribute added to shortcode
  731. * param list
  732. * @param $script_url - javascript file url which will be attached at the end of settings form.
  733. *
  734. * @return bool
  735. * @since 4.4
  736. */
  737. function vc_add_shortcode_param( $name, $form_field_callback, $script_url = null ) {
  738. return WpbakeryShortcodeParams::addField( $name, $form_field_callback, $script_url );
  739. }
  740. /**
  741. * Call hook for attribute.
  742. *
  743. * @param $name - attribute name
  744. * @param $param_settings - attribute settings from shortcode
  745. * @param $param_value - attribute value
  746. * @param $tag - attribute tag
  747. *
  748. * @return mixed|string - returns html which will be render in hook
  749. * @since 4.4
  750. */
  751. function vc_do_shortcode_param_settings_field( $name, $param_settings, $param_value, $tag ) {
  752. return WpbakeryShortcodeParams::renderSettingsField( $name, $param_settings, $param_value, $tag );
  753. }