PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Site/wp-content/plugins/js_composer/include/classes/editors/class-vc-backend-editor.php

https://gitlab.com/rubengrb/ws.vidrialum
PHP | 349 lines | 197 code | 30 blank | 122 comment | 17 complexity | 0a71daaacc3b933e40076f8100557421 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * WPBakery Visual Composer admin editor
  7. *
  8. * @package WPBakeryVisualComposer
  9. *
  10. */
  11. /**
  12. * VC backend editor.
  13. *
  14. * This editor is available on default Wp post/page admin edit page. ON admin_init callback adds meta box to
  15. * edit page.
  16. *
  17. * @since 4.2
  18. */
  19. class Vc_Backend_Editor implements Vc_Editor_Interface {
  20. /**
  21. * @var
  22. */
  23. protected $layout;
  24. /**
  25. * @var
  26. */
  27. public $post_custom_css;
  28. /**
  29. * @var bool|string $post - stores data about post.
  30. */
  31. public $post = false;
  32. /**
  33. * This method is called by Vc_Manager to register required action hooks for VC backend editor.
  34. *
  35. * @since 4.2
  36. * @access public
  37. */
  38. public function addHooksSettings() {
  39. // @todo - fix_roles do this only if be editor is enabled.
  40. add_action( 'wp_ajax_wpb_get_element_backend_html', array(
  41. &$this,
  42. 'elementBackendHtml',
  43. ) );
  44. // load backend editor
  45. if ( function_exists( 'add_theme_support' ) ) {
  46. add_theme_support( 'post-thumbnails' ); // @todo check is it needed?
  47. }
  48. add_action( 'add_meta_boxes', array(
  49. &$this,
  50. 'render',
  51. ), 5 );
  52. add_action( 'admin_print_scripts-post.php', array(
  53. &$this,
  54. 'printScriptsMessages',
  55. ) );
  56. add_action( 'admin_print_scripts-post-new.php', array(
  57. &$this,
  58. 'printScriptsMessages',
  59. ) );
  60. }
  61. /**
  62. * Calls add_meta_box to create Editor block. Block is rendered by WPBakeryVisualComposerLayout.
  63. *
  64. * @see WPBakeryVisualComposerLayout
  65. * @since 4.2
  66. * @access public
  67. */
  68. public function render( $post_type ) {
  69. if ( $this->isValidPostType( $post_type ) ) {
  70. $this->registerBackendJavascript();
  71. $this->registerBackendCss();
  72. // B.C:
  73. visual_composer()->registerAdminCss();
  74. visual_composer()->registerAdminJavascript();
  75. // meta box to render
  76. add_meta_box( 'wpb_visual_composer', __( 'Visual Composer', 'js_composer' ), array(
  77. &$this,
  78. 'renderEditor',
  79. ), $post_type, 'normal', 'high' );
  80. }
  81. }
  82. /**
  83. * Output html for backend editor meta box.
  84. *
  85. * @param null|Wp_Post $post
  86. *
  87. * @return bool
  88. */
  89. public function renderEditor( $post = null ) {
  90. /**
  91. * TODO: setter/getter for $post
  92. */
  93. if ( ! is_object( $post ) || 'WP_Post' !== get_class( $post ) || ! isset( $post->ID ) ) {
  94. return false;
  95. }
  96. $this->post = $post;
  97. $post_custom_css = strip_tags( get_post_meta( $post->ID, '_wpb_post_custom_css', true ) );
  98. $this->post_custom_css = $post_custom_css;
  99. vc_include_template( 'editors/backend_editor.tpl.php', array(
  100. 'editor' => $this,
  101. 'post' => $this->post,
  102. ) );
  103. add_action( 'admin_footer', array(
  104. &$this,
  105. 'renderEditorFooter',
  106. ) );
  107. do_action( 'vc_backend_editor_render' );
  108. return true;
  109. }
  110. /**
  111. * Output required html and js content for VC editor.
  112. *
  113. * Here comes panels, modals and js objects with data for mapped shortcodes.
  114. */
  115. public function renderEditorFooter() {
  116. vc_include_template( 'editors/partials/backend_editor_footer.tpl.php', array(
  117. 'editor' => $this,
  118. 'post' => $this->post,
  119. ) );
  120. do_action( 'vc_backend_editor_footer_render' );
  121. }
  122. /**
  123. * Check is post type is valid for rendering VC backend editor.
  124. *
  125. * @return bool
  126. */
  127. public function isValidPostType( $type = '' ) {
  128. if( 'vc_grid_item' === $type ) { return false; }
  129. return vc_check_post_type( ! empty( $type ) ? $type : get_post_type() );
  130. }
  131. /**
  132. * Enqueue required javascript libraries and css files.
  133. *
  134. * This method also setups reminder about license activation.
  135. *
  136. * @since 4.2
  137. * @access public
  138. */
  139. public function printScriptsMessages() {
  140. if ( ! vc_is_frontend_editor() && $this->isValidPostType( get_post_type() ) ) {
  141. $this->enqueueEditorScripts();
  142. }
  143. }
  144. /**
  145. * Enqueue required javascript libraries and css files.
  146. *
  147. * @since 4.8
  148. * @access public
  149. */
  150. public function enqueueEditorScripts() {
  151. if($this->editorEnabled()) {
  152. $this->enqueueJs();
  153. $this->enqueueCss();
  154. WPBakeryShortCodeFishBones::enqueueCss();
  155. WPBakeryShortCodeFishBones::enqueueJs();
  156. } else {
  157. wp_enqueue_script( 'vc-backend-actions-js' );
  158. $this->enqueueCss(); //needed for navbar @todo split
  159. }
  160. do_action( 'vc_backend_editor_enqueue_js_css' );
  161. }
  162. /**
  163. * Save generated shortcodes, html and visual composer status in posts meta.
  164. *
  165. * @deprecated 4.4
  166. * @since 3.0
  167. * @access public
  168. *
  169. * @param $post_id - current post id
  170. *
  171. * @return void
  172. */
  173. public function save( $post_id ) {
  174. _deprecated_function( '\Vc_Backend_Editor::save', '4.4 (will be removed in 4.10)', '\Vc_Post_Admin::save' );
  175. }
  176. /**
  177. * Create shortcode's string.
  178. *
  179. * @since 3.0
  180. * @access public
  181. * @deprecated 4.9
  182. */
  183. public function elementBackendHtml() {
  184. _deprecated_function( '\Vc_Backend_Editor::elementBackendHtml', '4.9 (will be removed in 4.10)' );
  185. vc_user_access()
  186. ->checkAdminNonce()
  187. ->validateDie()
  188. ->wpAny( 'edit_posts', 'edit_pages' )
  189. ->validateDie()
  190. ->part( 'backend_editor' )
  191. ->can()// checks is backend_editor enabled( !== false )
  192. ->validateDie();
  193. $data_element = vc_post_param( 'data_element' );
  194. if ( 'vc_column' === $data_element && null !== vc_post_param( 'data_width' ) ) {
  195. $output = do_shortcode( '[vc_column width="' . vc_post_param( 'data_width' ) . '"]' );
  196. echo $output;
  197. } elseif ( 'vc_row' === $data_element || 'vc_row_inner' === $data_element ) {
  198. $output = do_shortcode( '[' . $data_element . ']' );
  199. echo $output;
  200. } else {
  201. $output = do_shortcode( '[' . $data_element . ']' );
  202. echo $output;
  203. }
  204. die();
  205. }
  206. /**
  207. * @deprecated 4.8
  208. * @return string
  209. */
  210. public function showRulesValue() {
  211. global $current_user;
  212. wp_get_current_user();
  213. /** @var $settings - get use group access rules */
  214. $settings = vc_settings()->get( 'groups_access_rules' );
  215. $role = is_object( $current_user ) && isset( $current_user->roles[0] ) ? $current_user->roles[0] : '';
  216. return isset( $settings[ $role ]['show'] ) ? $settings[ $role ]['show'] : '';
  217. }
  218. public function registerBackendJavascript() {
  219. // editor can be disabled but fe can be enabled. so we currently need this file. @todo maybe make backend-disabled.min.js
  220. wp_register_script( 'vc-backend-actions-js', vc_asset_url( 'js/dist/backend-actions.min.js' ), array(
  221. 'jquery',
  222. 'backbone',
  223. 'underscore',
  224. ), WPB_VC_VERSION, true );
  225. wp_register_script( 'vc-backend-min-js', vc_asset_url( 'js/dist/backend.min.js' ), array( 'vc-backend-actions-js' ), WPB_VC_VERSION, true );
  226. // used in tta shortcodes, and panels.
  227. wp_register_script( 'vc_accordion_script', vc_asset_url( 'lib/vc_accordion/vc-accordion.min.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  228. wp_register_script( 'wpb_php_js', vc_asset_url( 'lib/php.default/php.default.min.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  229. // used as polyfill for JSON.stringify and etc
  230. wp_register_script( 'wpb_json-js', vc_asset_url( 'lib/bower/json-js/json2.min.js' ), array(), WPB_VC_VERSION, true );
  231. // used in post settings editor
  232. wp_register_script( 'ace-editor', vc_asset_url( 'lib/bower/ace-builds/src-min-noconflict/ace.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  233. wp_register_script( 'webfont', '//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js' ); // Google Web Font CDN
  234. wp_localize_script( 'vc-backend-actions-js', 'i18nLocale', visual_composer()->getEditorsLocale() );
  235. }
  236. public function registerBackendCss() {
  237. wp_register_style( 'js_composer', vc_asset_url( 'css/js_composer_backend_editor.min.css' ), array(), WPB_VC_VERSION, false );
  238. if ( $this->editorEnabled() ) {
  239. /**
  240. * @deprecated, used for accordions/tabs/tours
  241. */
  242. wp_register_style( 'ui-custom-theme', vc_asset_url( 'css/ui-custom-theme/jquery-ui-less.custom.min.css' ), array(), WPB_VC_VERSION, false );
  243. /**
  244. * @todo check vc_add-element-deprecated-warning for fa icon usage ( set to our font )
  245. * also used in vc_icon shortcode
  246. */
  247. wp_register_style( 'font-awesome', vc_asset_url( 'lib/bower/font-awesome/css/font-awesome.min.css' ), array(), WPB_VC_VERSION, false );
  248. /**
  249. * @todo check for usages
  250. * definetelly used in edit form param: css_animation, but curreny vc_add_shortcode_param doesn't accept css [ @todo refactor that ]
  251. */
  252. wp_register_style( 'animate-css', vc_asset_url( 'lib/bower/animate-css/animate.min.css' ), array(), WPB_VC_VERSION, false );
  253. }
  254. }
  255. public function enqueueJs() {
  256. $wp_dependencies = array(
  257. 'jquery',
  258. 'underscore',
  259. 'backbone',
  260. 'media-views',
  261. 'media-editor',
  262. 'wp-pointer',
  263. 'mce-view',
  264. 'wp-color-picker',
  265. 'jquery-ui-sortable',
  266. 'jquery-ui-droppable',
  267. 'jquery-ui-draggable',
  268. 'jquery-ui-autocomplete',
  269. 'jquery-ui-resizable',
  270. // used in @deprecated tabs
  271. 'jquery-ui-tabs',
  272. 'jquery-ui-accordion',
  273. );
  274. $dependencies = array(
  275. 'vc_accordion_script',
  276. 'wpb_php_js',
  277. // used in our files [e.g. edit form saving sprintf]
  278. 'wpb_json-js',
  279. 'ace-editor',
  280. 'webfont',
  281. 'vc-backend-min-js',
  282. );
  283. // This workaround will allow to disable any of dependency on-the-fly
  284. foreach ( $wp_dependencies as $dependency ) {
  285. wp_enqueue_script( $dependency );
  286. }
  287. foreach ( $dependencies as $dependency ) {
  288. wp_enqueue_script( $dependency );
  289. }
  290. }
  291. public function enqueueCss() {
  292. $wp_dependencies = array(
  293. 'wp-color-picker',
  294. 'farbtastic',
  295. // deprecated for tabs/accordion
  296. 'ui-custom-theme',
  297. // used in deprecated message and also in vc-icon shortcode
  298. 'font-awesome',
  299. // used in css_animation edit form param
  300. 'animate-css',
  301. );
  302. $dependencies = array(
  303. 'js_composer',
  304. );
  305. // This workaround will allow to disable any of dependency on-the-fly
  306. foreach ( $wp_dependencies as $dependency ) {
  307. wp_enqueue_style( $dependency );
  308. }
  309. foreach ( $dependencies as $dependency ) {
  310. wp_enqueue_style( $dependency );
  311. }
  312. }
  313. /**
  314. * @return bool
  315. */
  316. public function editorEnabled() {
  317. return vc_user_access()->part( 'backend_editor' )->can()->get();
  318. }
  319. }