helpers_factory.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * WPBakery WPBakery Page Builder Main manager.
  7. *
  8. * @package WPBakeryPageBuilder
  9. * @since 4.2
  10. */
  11. if ( ! function_exists( 'vc_manager' ) ) {
  12. /**
  13. * WPBakery Page Builder manager.
  14. * @return Vc_Manager
  15. * @since 4.2
  16. */
  17. function vc_manager() {
  18. return Vc_Manager::getInstance();
  19. }
  20. }
  21. if ( ! function_exists( 'visual_composer' ) ) {
  22. /**
  23. * WPBakery Page Builder instance.
  24. * @return Vc_Base
  25. * @since 4.2
  26. */
  27. function visual_composer() {
  28. return vc_manager()->vc();
  29. }
  30. }
  31. if ( ! function_exists( 'vc_mapper' ) ) {
  32. /**
  33. * Shorthand for Vc Mapper.
  34. * @return Vc_Mapper
  35. * @since 4.2
  36. */
  37. function vc_mapper() {
  38. return vc_manager()->mapper();
  39. }
  40. }
  41. if ( ! function_exists( 'vc_settings' ) ) {
  42. /**
  43. * Shorthand for WPBakery Page Builder settings.
  44. * @return Vc_Settings
  45. * @since 4.2
  46. */
  47. function vc_settings() {
  48. return vc_manager()->settings();
  49. }
  50. }
  51. if ( ! function_exists( 'vc_license' ) ) {
  52. /**
  53. * Get License manager
  54. * @return Vc_License
  55. * @since 4.2
  56. */
  57. function vc_license() {
  58. return vc_manager()->license();
  59. }
  60. }
  61. if ( ! function_exists( 'vc_automapper' ) ) {
  62. /**
  63. * @return Vc_Automapper
  64. * @since 4.2
  65. */
  66. function vc_automapper() {
  67. return vc_manager()->automapper();
  68. }
  69. }
  70. if ( ! function_exists( 'vc_frontend_editor' ) ) {
  71. /**
  72. * Shorthand for VC frontend editor
  73. * @return Vc_Frontend_Editor
  74. * @since 4.2
  75. */
  76. function vc_frontend_editor() {
  77. return vc_manager()->frontendEditor();
  78. }
  79. }
  80. if ( ! function_exists( 'vc_backend_editor' ) ) {
  81. /**
  82. * Shorthand for VC frontend editor
  83. * @return Vc_Backend_Editor
  84. * @since 4.2
  85. */
  86. function vc_backend_editor() {
  87. return vc_manager()->backendEditor();
  88. }
  89. }
  90. if ( ! function_exists( 'vc_updater' ) ) {
  91. /**
  92. * @return Vc_Updater
  93. * @since 4.2
  94. */
  95. function vc_updater() {
  96. return vc_manager()->updater();
  97. }
  98. }
  99. if ( ! function_exists( 'vc_is_network_plugin' ) ) {
  100. /**
  101. * Vc is network plugin or not.
  102. * @return bool
  103. * @since 4.2
  104. */
  105. function vc_is_network_plugin() {
  106. return vc_manager()->isNetworkPlugin();
  107. }
  108. }
  109. if ( ! function_exists( 'vc_path_dir' ) ) {
  110. /**
  111. * Get file/directory path in Vc.
  112. *
  113. * @param string $name - path name
  114. * @param string $file
  115. *
  116. * @return string
  117. * @since 4.2
  118. */
  119. function vc_path_dir( $name, $file = '' ) {
  120. return vc_manager()->path( $name, $file );
  121. }
  122. }
  123. if ( ! function_exists( 'vc_asset_url' ) ) {
  124. /**
  125. * Get full url for assets.
  126. *
  127. * @param string $file
  128. *
  129. * @return string
  130. * @since 4.2
  131. */
  132. function vc_asset_url( $file ) {
  133. return vc_manager()->assetUrl( $file );
  134. }
  135. }
  136. if ( ! function_exists( 'vc_upload_dir' ) ) {
  137. /**
  138. * Temporary files upload dir;
  139. * @return string
  140. * @since 4.2
  141. */
  142. function vc_upload_dir() {
  143. return vc_manager()->uploadDir();
  144. }
  145. }
  146. if ( ! function_exists( 'vc_template' ) ) {
  147. /**
  148. * @param $file
  149. *
  150. * @return string
  151. * @since 4.2
  152. */
  153. function vc_template( $file ) {
  154. return vc_path_dir( 'TEMPLATES_DIR', $file );
  155. }
  156. }
  157. if ( ! function_exists( 'vc_post_param' ) ) {
  158. /**
  159. * Get param value from $_POST if exists.
  160. *
  161. * @param $param
  162. * @param $default
  163. *
  164. * @param bool $check
  165. * @return null|string - null for undefined param.
  166. * @since 4.2
  167. */
  168. function vc_post_param( $param, $default = null, $check = false ) {
  169. if ( 'admin' === $check ) {
  170. check_admin_referer();
  171. } elseif ( 'ajax' === $check ) {
  172. check_ajax_referer();
  173. }
  174. return isset( $_POST[ $param ] ) ? wp_unslash( $_POST[ $param ] ) : $default;
  175. }
  176. }
  177. if ( ! function_exists( 'vc_get_param' ) ) {
  178. /**
  179. * Get param value from $_GET if exists.
  180. *
  181. * @param string $param
  182. * @param $default
  183. *
  184. * @param bool $check
  185. * @return null|string - null for undefined param.
  186. * @since 4.2
  187. */
  188. function vc_get_param( $param, $default = null, $check = false ) {
  189. if ( 'admin' === $check ) {
  190. check_admin_referer();
  191. } elseif ( 'ajax' === $check ) {
  192. check_ajax_referer();
  193. }
  194. // @codingStandardsIgnoreLine
  195. return isset( $_GET[ $param ] ) ? wp_unslash( $_GET[ $param ] ) : $default;
  196. }
  197. }
  198. if ( ! function_exists( 'vc_request_param' ) ) {
  199. /**
  200. * Get param value from $_REQUEST if exists.
  201. *
  202. * @param $param
  203. * @param $default
  204. *
  205. * @param bool $check
  206. * @return null|string - null for undefined param.
  207. * @since 4.4
  208. */
  209. function vc_request_param( $param, $default = null, $check = false ) {
  210. if ( 'admin' === $check ) {
  211. check_admin_referer();
  212. } elseif ( 'ajax' === $check ) {
  213. check_ajax_referer();
  214. }
  215. // @codingStandardsIgnoreLine
  216. return isset( $_REQUEST[ $param ] ) ? wp_unslash( $_REQUEST[ $param ] ) : $default;
  217. }
  218. }
  219. if ( ! function_exists( 'vc_is_frontend_editor' ) ) {
  220. /**
  221. * @return bool
  222. * @since 4.2
  223. */
  224. function vc_is_frontend_editor() {
  225. return 'admin_frontend_editor' === vc_mode();
  226. }
  227. }
  228. if ( ! function_exists( 'vc_is_page_editable' ) ) {
  229. /**
  230. * @return bool
  231. * @since 4.2
  232. */
  233. function vc_is_page_editable() {
  234. return 'page_editable' === vc_mode();
  235. }
  236. }
  237. if ( ! function_exists( 'vc_action' ) ) {
  238. /**
  239. * Get VC special action param.
  240. * @return string|null
  241. * @since 4.2
  242. */
  243. function vc_action() {
  244. $vc_action = wp_strip_all_tags( vc_request_param( 'vc_action' ) );
  245. return $vc_action;
  246. }
  247. }
  248. if ( ! function_exists( 'vc_is_inline' ) ) {
  249. /**
  250. * Get is inline or not.
  251. * @return bool
  252. * @since 4.2
  253. */
  254. function vc_is_inline() {
  255. global $vc_is_inline;
  256. if ( is_null( $vc_is_inline ) ) {
  257. $vc_is_inline = ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) && 'vc_inline' === vc_action() || ! is_null( vc_request_param( 'vc_inline' ) ) || 'true' === vc_request_param( 'vc_editable' );
  258. }
  259. return $vc_is_inline;
  260. }
  261. }
  262. if ( ! function_exists( 'vc_is_frontend_ajax' ) ) {
  263. /**
  264. * @return bool
  265. * @since 4.2
  266. */
  267. function vc_is_frontend_ajax() {
  268. return 'true' === vc_post_param( 'vc_inline' ) || vc_get_param( 'vc_inline' );
  269. }
  270. }
  271. /**
  272. * @depreacted since 4.8 ( use vc_is_frontend_editor )
  273. * @return bool
  274. * @since 4.2
  275. */
  276. function vc_is_editor() {
  277. return vc_is_frontend_editor();
  278. }
  279. /**
  280. * @param $value
  281. * @param bool $encode
  282. *
  283. * @return string
  284. * @since 4.2
  285. */
  286. function vc_value_from_safe( $value, $encode = false ) {
  287. // @codingStandardsIgnoreLine
  288. $value = preg_match( '/^#E\-8_/', $value ) ? rawurldecode( base64_decode( preg_replace( '/^#E\-8_/', '', $value ) ) ) : $value;
  289. if ( $encode ) {
  290. $value = htmlentities( $value, ENT_COMPAT, 'UTF-8' );
  291. }
  292. return $value;
  293. }
  294. /**
  295. * @param bool $disable
  296. * @since 4.2
  297. *
  298. */
  299. function vc_disable_automapper( $disable = true ) {
  300. vc_automapper()->setDisabled( $disable );
  301. }
  302. /**
  303. * @return bool
  304. * @since 4.2
  305. */
  306. function vc_automapper_is_disabled() {
  307. return vc_automapper()->disabled();
  308. }
  309. /**
  310. * @param $param
  311. * @param $value
  312. *
  313. * @return mixed|string
  314. * @since 4.2
  315. */
  316. function vc_get_dropdown_option( $param, $value ) {
  317. if ( '' === $value && is_array( $param['value'] ) ) {
  318. $value = array_shift( $param['value'] );
  319. }
  320. if ( is_array( $value ) ) {
  321. reset( $value );
  322. $value = isset( $value['value'] ) ? $value['value'] : current( $value );
  323. }
  324. $value = preg_replace( '/\s/', '_', $value );
  325. return ( '' !== $value ? $value : '' );
  326. }
  327. /**
  328. * @param $prefix
  329. * @param $color
  330. *
  331. * @return string
  332. * @since 4.2
  333. */
  334. function vc_get_css_color( $prefix, $color ) {
  335. $rgb_color = preg_match( '/rgba/', $color ) ? preg_replace( array(
  336. '/\s+/',
  337. '/^rgba\((\d+)\,(\d+)\,(\d+)\,([\d\.]+)\)$/',
  338. ), array(
  339. '',
  340. 'rgb($1,$2,$3)',
  341. ), $color ) : $color;
  342. $string = $prefix . ':' . $rgb_color . ';';
  343. if ( $rgb_color !== $color ) {
  344. $string .= $prefix . ':' . $color . ';';
  345. }
  346. return $string;
  347. }
  348. /**
  349. * @param $param_value
  350. * @param string $prefix
  351. *
  352. * @return string
  353. * @since 4.2
  354. */
  355. function vc_shortcode_custom_css_class( $param_value, $prefix = '' ) {
  356. $css_class = preg_match( '/\s*\.([^\{]+)\s*\{\s*([^\}]+)\s*\}\s*/', $param_value ) ? $prefix . preg_replace( '/\s*\.([^\{]+)\s*\{\s*([^\}]+)\s*\}\s*/', '$1', $param_value ) : '';
  357. return $css_class;
  358. }
  359. /**
  360. * @param $subject
  361. * @param $property
  362. * @param bool|false $strict
  363. *
  364. * @return bool
  365. * @since 4.9
  366. */
  367. function vc_shortcode_custom_css_has_property( $subject, $property, $strict = false ) {
  368. $styles = array();
  369. $pattern = '/\{([^\}]*?)\}/i';
  370. preg_match( $pattern, $subject, $styles );
  371. if ( array_key_exists( 1, $styles ) ) {
  372. $styles = explode( ';', $styles[1] );
  373. }
  374. $new_styles = array();
  375. foreach ( $styles as $val ) {
  376. $val = explode( ':', $val );
  377. if ( is_array( $property ) ) {
  378. foreach ( $property as $prop ) {
  379. $pos = strpos( $val[0], $prop );
  380. $full = ( $strict ) ? ( 0 === $pos && strlen( $val[0] ) === strlen( $prop ) ) : true;
  381. if ( false !== $pos && $full ) {
  382. $new_styles[] = $val;
  383. }
  384. }
  385. } else {
  386. $pos = strpos( $val[0], $property );
  387. $full = ( $strict ) ? ( 0 === $pos && strlen( $val[0] ) === strlen( $property ) ) : true;
  388. if ( false !== $pos && $full ) {
  389. $new_styles[] = $val;
  390. }
  391. }
  392. }
  393. return ! empty( $new_styles );
  394. }
  395. /**
  396. * Plugin name for VC.
  397. *
  398. * @return string
  399. * @since 4.2
  400. */
  401. function vc_plugin_name() {
  402. return vc_manager()->pluginName();
  403. }
  404. /**
  405. * @param $filename
  406. *
  407. * @return bool|mixed|string
  408. * @since 4.4.3 used in vc_base when getting an custom css output
  409. *
  410. */
  411. function vc_file_get_contents( $filename ) {
  412. global $wp_filesystem;
  413. if ( empty( $wp_filesystem ) ) {
  414. require_once ABSPATH . '/wp-admin/includes/file.php';
  415. WP_Filesystem( false, false, true );
  416. }
  417. /** @var WP_Filesystem_Base $wp_filesystem */
  418. $output = '';
  419. if ( is_object( $wp_filesystem ) ) {
  420. $output = $wp_filesystem->get_contents( $filename );
  421. }
  422. if ( ! $output ) {
  423. // @codingStandardsIgnoreLine
  424. $output = file_get_contents( $filename );
  425. }
  426. return $output;
  427. }
  428. /**
  429. * HowTo: vc_role_access()->who('administrator')->with('editor')->can('frontend_editor');
  430. * @return Vc_Role_Access;
  431. * @since 4.8
  432. */
  433. function vc_role_access() {
  434. return vc_manager()->getRoleAccess();
  435. }
  436. /**
  437. * Get access manager for current user.
  438. * HowTo: vc_user_access()->->with('editor')->can('frontend_editor');
  439. * @return Vc_Current_User_Access;
  440. * @since 4.8
  441. */
  442. function vc_user_access() {
  443. return vc_manager()->getCurrentUserAccess();
  444. }
  445. /**
  446. * @return array
  447. * @throws \Exception
  448. */
  449. function vc_user_roles_get_all() {
  450. require_once vc_path_dir( 'SETTINGS_DIR', 'class-vc-roles.php' );
  451. $vc_roles = new Vc_Roles();
  452. $capabilities = array();
  453. foreach ( $vc_roles->getParts() as $part ) {
  454. $partObj = vc_user_access()->part( $part );
  455. $capabilities[ $part ] = array(
  456. 'state' => $partObj->getState(),
  457. 'state_key' => $partObj->getStateKey(),
  458. 'capabilities' => $partObj->getAllCaps(),
  459. );
  460. }
  461. return $capabilities;
  462. }
  463. /**
  464. * @param $data
  465. *
  466. * @return string
  467. */
  468. function vc_generate_nonce( $data ) {
  469. return wp_create_nonce( is_array( $data ) ? ( 'vc-nonce-' . implode( '|', $data ) ) : ( 'vc-nonce-' . $data ) );
  470. }
  471. /**
  472. * @param $nonce
  473. * @param $data
  474. *
  475. * @return bool
  476. */
  477. function vc_verify_nonce( $nonce, $data ) {
  478. return (bool) wp_verify_nonce( $nonce, ( is_array( $data ) ? ( 'vc-nonce-' . implode( '|', $data ) ) : ( 'vc-nonce-' . $data ) ) );
  479. }
  480. /**
  481. * @param $nonce
  482. *
  483. * @return bool
  484. */
  485. function vc_verify_admin_nonce( $nonce = '' ) {
  486. return (bool) vc_verify_nonce( ! empty( $nonce ) ? $nonce : vc_request_param( '_vcnonce' ), 'vc-admin-nonce' );
  487. }
  488. /**
  489. * @param $nonce
  490. *
  491. * @return bool
  492. */
  493. function vc_verify_public_nonce( $nonce = '' ) {
  494. return (bool) vc_verify_nonce( ( ! empty( $nonce ) ? $nonce : vc_request_param( '_vcnonce' ) ), 'vc-public-nonce' );
  495. }
  496. /**
  497. * @param $type
  498. * @return bool|mixed|void
  499. * @throws \Exception
  500. */
  501. function vc_check_post_type( $type ) {
  502. if ( empty( $type ) ) {
  503. $type = get_post_type();
  504. }
  505. $valid = apply_filters( 'vc_check_post_type_validation', null, $type );
  506. if ( is_null( $valid ) ) {
  507. if ( is_multisite() && is_super_admin() ) {
  508. return true;
  509. }
  510. $state = vc_user_access()->part( 'post_types' )->getState();
  511. if ( null === $state ) {
  512. return in_array( $type, vc_default_editor_post_types(), true );
  513. } elseif ( true === $state && ! in_array( $type, vc_default_editor_post_types(), true ) ) {
  514. $valid = false;
  515. } else {
  516. $valid = vc_user_access()->part( 'post_types' )->can( $type )->get();
  517. }
  518. }
  519. return $valid;
  520. }
  521. /**
  522. * @param $shortcode
  523. * @return bool|mixed|void
  524. */
  525. function vc_user_access_check_shortcode_edit( $shortcode ) {
  526. $do_check = apply_filters( 'vc_user_access_check-shortcode_edit', null, $shortcode );
  527. if ( is_null( $do_check ) ) {
  528. $state_check = vc_user_access()->part( 'shortcodes' )->checkStateAny( true, 'edit', null )->get();
  529. if ( $state_check ) {
  530. return true;
  531. } else {
  532. return vc_user_access()->part( 'shortcodes' )->canAny( $shortcode . '_all', $shortcode . '_edit' )->get();
  533. }
  534. } else {
  535. return $do_check;
  536. }
  537. }
  538. /**
  539. * @param $shortcode
  540. * @return bool|mixed|void
  541. * @throws \Exception
  542. */
  543. function vc_user_access_check_shortcode_all( $shortcode ) {
  544. $do_check = apply_filters( 'vc_user_access_check-shortcode_all', null, $shortcode );
  545. if ( is_null( $do_check ) ) {
  546. return vc_user_access()->part( 'shortcodes' )->checkStateAny( true, 'custom', null )->can( $shortcode . '_all' )->get();
  547. } else {
  548. return $do_check;
  549. }
  550. }
  551. /**
  552. * htmlspecialchars_decode_deep
  553. * Call the htmlspecialchars_decode to a gived multilevel array
  554. *
  555. * @param mixed $value The value to be stripped.
  556. *
  557. * @return mixed Stripped value.
  558. * @since 4.8
  559. *
  560. */
  561. function vc_htmlspecialchars_decode_deep( $value ) {
  562. if ( is_array( $value ) ) {
  563. $value = array_map( 'vc_htmlspecialchars_decode_deep', $value );
  564. } elseif ( is_object( $value ) ) {
  565. $vars = get_object_vars( $value );
  566. foreach ( $vars as $key => $data ) {
  567. $value->{$key} = vc_htmlspecialchars_decode_deep( $data );
  568. }
  569. } elseif ( is_string( $value ) ) {
  570. $value = htmlspecialchars_decode( $value );
  571. }
  572. return $value;
  573. }
  574. /**
  575. * @param $str
  576. * @return mixed
  577. */
  578. function vc_str_remove_protocol( $str ) {
  579. return str_replace( array(
  580. 'https://',
  581. 'http://',
  582. ), '//', $str );
  583. }