PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/mattswann/launch-housing
PHP | 356 lines | 204 code | 30 blank | 122 comment | 17 complexity | 4ab36fecf54f312463dad727846ed0ed 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. return vc_check_post_type( ! empty( $type ) ? $type : get_post_type() );
  129. }
  130. /**
  131. * Enqueue required javascript libraries and css files.
  132. *
  133. * This method also setups reminder about license activation.
  134. *
  135. * @since 4.2
  136. * @access public
  137. */
  138. public function printScriptsMessages() {
  139. if ( ! vc_is_frontend_editor() && $this->isValidPostType() ) {
  140. if ( vc_user_access()
  141. ->wpAny( 'manage_options' )
  142. ->part( 'settings' )
  143. ->can( 'vc-updater-tab' )
  144. ->get()
  145. ) {
  146. vc_license()->setupReminder();
  147. }
  148. $this->enqueueEditorScripts();
  149. }
  150. }
  151. /**
  152. * Enqueue required javascript libraries and css files.
  153. *
  154. * @since 4.8
  155. * @access public
  156. */
  157. public function enqueueEditorScripts() {
  158. if($this->editorEnabled()) {
  159. $this->enqueueJs();
  160. $this->enqueueCss();
  161. WPBakeryShortCodeFishBones::enqueueCss();
  162. WPBakeryShortCodeFishBones::enqueueJs();
  163. } else {
  164. wp_enqueue_script( 'vc-backend-actions-js' );
  165. $this->enqueueCss(); //needed for navbar @todo split
  166. }
  167. do_action( 'vc_backend_editor_enqueue_js_css' );
  168. }
  169. /**
  170. * Save generated shortcodes, html and visual composer status in posts meta.
  171. *
  172. * @deprecated 4.4
  173. * @since 3.0
  174. * @access public
  175. *
  176. * @param $post_id - current post id
  177. *
  178. * @return void
  179. */
  180. public function save( $post_id ) {
  181. _deprecated_function( '\Vc_Backend_Editor::save', '4.4 (will be removed in 4.10)', '\Vc_Post_Admin::save' );
  182. }
  183. /**
  184. * Create shortcode's string.
  185. *
  186. * @since 3.0
  187. * @access public
  188. * @deprecated 4.9
  189. */
  190. public function elementBackendHtml() {
  191. _deprecated_function( '\Vc_Backend_Editor::elementBackendHtml', '4.9 (will be removed in 4.10)' );
  192. vc_user_access()
  193. ->checkAdminNonce()
  194. ->validateDie()
  195. ->wpAny( 'edit_posts', 'edit_pages' )
  196. ->validateDie()
  197. ->part( 'backend_editor' )
  198. ->can()// checks is backend_editor enabled( !== false )
  199. ->validateDie();
  200. $data_element = vc_post_param( 'data_element' );
  201. if ( 'vc_column' === $data_element && null !== vc_post_param( 'data_width' ) ) {
  202. $output = do_shortcode( '[vc_column width="' . vc_post_param( 'data_width' ) . '"]' );
  203. echo $output;
  204. } elseif ( 'vc_row' === $data_element || 'vc_row_inner' === $data_element ) {
  205. $output = do_shortcode( '[' . $data_element . ']' );
  206. echo $output;
  207. } else {
  208. $output = do_shortcode( '[' . $data_element . ']' );
  209. echo $output;
  210. }
  211. die();
  212. }
  213. /**
  214. * @deprecated 4.8
  215. * @return string
  216. */
  217. public function showRulesValue() {
  218. global $current_user;
  219. get_currentuserinfo();
  220. /** @var $settings - get use group access rules */
  221. $settings = vc_settings()->get( 'groups_access_rules' );
  222. $role = is_object( $current_user ) && isset( $current_user->roles[0] ) ? $current_user->roles[0] : '';
  223. return isset( $settings[ $role ]['show'] ) ? $settings[ $role ]['show'] : '';
  224. }
  225. public function registerBackendJavascript() {
  226. // editor can be disabled but fe can be enabled. so we currently need this file. @todo maybe make backend-disabled.min.js
  227. wp_register_script( 'vc-backend-actions-js', vc_asset_url( 'js/dist/backend-actions.min.js' ), array(
  228. 'jquery',
  229. 'backbone',
  230. 'underscore',
  231. ), WPB_VC_VERSION, true );
  232. wp_register_script( 'vc-backend-min-js', vc_asset_url( 'js/dist/backend.min.js' ), array( 'vc-backend-actions-js' ), WPB_VC_VERSION, true );
  233. // used in tta shortcodes, and panels.
  234. wp_register_script( 'vc_accordion_script', vc_asset_url( 'lib/vc_accordion/vc-accordion.min.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  235. wp_register_script( 'wpb_php_js', vc_asset_url( 'lib/php.default/php.default.min.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  236. // used as polyfill for JSON.stringify and etc
  237. wp_register_script( 'wpb_json-js', vc_asset_url( 'lib/bower/json-js/json2.min.js' ), array(), WPB_VC_VERSION, true );
  238. // used in post settings editor
  239. wp_register_script( 'ace-editor', vc_asset_url( 'lib/bower/ace-builds/src-min-noconflict/ace.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  240. wp_register_script( 'webfont', '//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js' ); // Google Web Font CDN
  241. wp_localize_script( 'vc-backend-actions-js', 'i18nLocale', visual_composer()->getEditorsLocale() );
  242. }
  243. public function registerBackendCss() {
  244. wp_register_style( 'js_composer', vc_asset_url( 'css/js_composer_backend_editor.min.css' ), array(), WPB_VC_VERSION, false );
  245. if ( $this->editorEnabled() ) {
  246. /**
  247. * @deprecated, used for accordions/tabs/tours
  248. */
  249. wp_register_style( 'ui-custom-theme', vc_asset_url( 'css/ui-custom-theme/jquery-ui-less.custom.min.css' ), array(), WPB_VC_VERSION, false );
  250. /**
  251. * @todo check vc_add-element-deprecated-warning for fa icon usage ( set to our font )
  252. * also used in vc_icon shortcode
  253. */
  254. wp_register_style( 'font-awesome', vc_asset_url( 'lib/bower/font-awesome/css/font-awesome.min.css' ), array(), WPB_VC_VERSION, false );
  255. /**
  256. * @todo check for usages
  257. * definetelly used in edit form param: css_animation, but curreny vc_add_shortcode_param doesn't accept css [ @todo refactor that ]
  258. */
  259. wp_register_style( 'animate-css', vc_asset_url( 'lib/bower/animate-css/animate.min.css' ), array(), WPB_VC_VERSION, false );
  260. }
  261. }
  262. public function enqueueJs() {
  263. $wp_dependencies = array(
  264. 'jquery',
  265. 'underscore',
  266. 'backbone',
  267. 'media-views',
  268. 'media-editor',
  269. 'wp-pointer',
  270. 'mce-view',
  271. 'wp-color-picker',
  272. 'jquery-ui-sortable',
  273. 'jquery-ui-droppable',
  274. 'jquery-ui-draggable',
  275. 'jquery-ui-autocomplete',
  276. 'jquery-ui-resizable',
  277. // used in @deprecated tabs
  278. 'jquery-ui-tabs',
  279. 'jquery-ui-accordion',
  280. );
  281. $dependencies = array(
  282. 'vc_accordion_script',
  283. 'wpb_php_js',
  284. // used in our files [e.g. edit form saving sprintf]
  285. 'wpb_json-js',
  286. 'ace-editor',
  287. 'webfont',
  288. 'vc-backend-min-js',
  289. );
  290. // This workaround will allow to disable any of dependency on-the-fly
  291. foreach ( $wp_dependencies as $dependency ) {
  292. wp_enqueue_script( $dependency );
  293. }
  294. foreach ( $dependencies as $dependency ) {
  295. wp_enqueue_script( $dependency );
  296. }
  297. }
  298. public function enqueueCss() {
  299. $wp_dependencies = array(
  300. 'wp-color-picker',
  301. 'farbtastic',
  302. // deprecated for tabs/accordion
  303. 'ui-custom-theme',
  304. // used in deprecated message and also in vc-icon shortcode
  305. 'font-awesome',
  306. // used in css_animation edit form param
  307. 'animate-css',
  308. );
  309. $dependencies = array(
  310. 'js_composer',
  311. );
  312. // This workaround will allow to disable any of dependency on-the-fly
  313. foreach ( $wp_dependencies as $dependency ) {
  314. wp_enqueue_style( $dependency );
  315. }
  316. foreach ( $dependencies as $dependency ) {
  317. wp_enqueue_style( $dependency );
  318. }
  319. }
  320. /**
  321. * @return bool
  322. */
  323. public function editorEnabled() {
  324. return vc_user_access()->part( 'backend_editor' )->can()->get();
  325. }
  326. }