vc-grid-item-editor.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. global $vc_grid_item_editor;
  6. /**
  7. * Creates new post type for grid_editor.
  8. *
  9. * @since 4.4
  10. */
  11. function vc_grid_item_editor_create_post_type() {
  12. if ( is_admin() ) {
  13. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/editor/class-vc-grid-item-editor.php' );
  14. Vc_Grid_Item_Editor::createPostType();
  15. add_action( 'vc_menu_page_build', 'vc_gitem_add_submenu_page' );
  16. // TODO: add check vendor is active
  17. add_filter( 'vc_vendor_qtranslate_enqueue_js_backend', 'vc_vendor_qtranslate_enqueue_js_backend_grid_editor' );
  18. }
  19. }
  20. /**
  21. * @since 4.5
  22. */
  23. function vc_vendor_qtranslate_enqueue_js_backend_grid_editor() {
  24. return true;
  25. }
  26. /**
  27. * Set required objects to render editor for grid item
  28. *
  29. * @since 4.4
  30. */
  31. function vc_grid_item_editor_init() {
  32. global $vc_grid_item_editor;
  33. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/editor/class-vc-grid-item-editor.php' );
  34. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/class-wpb-map-grid-item.php' );
  35. $vc_grid_item_editor = new Vc_Grid_Item_Editor();
  36. $vc_grid_item_editor->addMetaBox();
  37. add_action( 'wp_ajax_vc_grid_item_editor_load_template_preview', array(
  38. &$vc_grid_item_editor,
  39. 'renderTemplatePreview',
  40. ) );
  41. $vc_grid_item_editor->addHooksSettings();
  42. }
  43. /**
  44. * Render preview for grid item
  45. * @since 4.4
  46. */
  47. function vc_grid_item_render_preview() {
  48. vc_user_access()->checkAdminNonce()->validateDie()->wpAny( array(
  49. 'edit_post',
  50. (int) vc_request_param( 'post_id' ),
  51. ) )->validateDie()->part( 'grid_builder' )->can()->validateDie();
  52. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/class-vc-grid-item.php' );
  53. $grid_item = new Vc_Grid_Item();
  54. $grid_item->mapShortcodes();
  55. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/editor/class-vc-grid-item-preview.php' );
  56. $vcGridPreview = new Vc_Grid_Item_Preview();
  57. add_filter( 'vc_gitem_template_attribute_post_image_background_image_css_value', array(
  58. $vcGridPreview,
  59. 'addCssBackgroundImage',
  60. ) );
  61. add_filter( 'vc_gitem_template_attribute_post_image_url_value', array(
  62. $vcGridPreview,
  63. 'addImageUrl',
  64. ) );
  65. add_filter( 'vc_gitem_template_attribute_post_image_html', array(
  66. $vcGridPreview,
  67. 'addImage',
  68. ) );
  69. add_filter( 'vc_gitem_attribute_featured_image_img', array(
  70. $vcGridPreview,
  71. 'addPlaceholderImage',
  72. ) );
  73. add_filter( 'vc_gitem_post_data_get_link_real_link', array(
  74. $vcGridPreview,
  75. 'disableRealContentLink',
  76. ), 10, 4 );
  77. add_filter( 'vc_gitem_post_data_get_link_link', array(
  78. $vcGridPreview,
  79. 'disableContentLink',
  80. ), 10, 3 );
  81. add_filter( 'vc_gitem_zone_image_block_link', array(
  82. $vcGridPreview,
  83. 'disableGitemZoneLink',
  84. ) );
  85. $vcGridPreview->render();
  86. die();
  87. }
  88. /**
  89. * Map grid element shortcodes.
  90. *
  91. * @since 4.5
  92. */
  93. function vc_grid_item_map_shortcodes() {
  94. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/class-vc-grid-item.php' );
  95. $grid_item = new Vc_Grid_Item();
  96. $grid_item->mapShortcodes();
  97. vc_mapper()->setCheckForAccess( false );
  98. }
  99. /**
  100. * Get current post type
  101. *
  102. * @return null|string
  103. */
  104. function vc_grid_item_get_post_type() {
  105. $post_type = null;
  106. if ( vc_request_param( 'post_type' ) ) {
  107. $post_type = vc_request_param( 'post_type' );
  108. } elseif ( vc_request_param( 'post' ) ) {
  109. $post = get_post( vc_request_param( 'post' ) );
  110. $post_type = $post instanceof WP_Post && $post->post_type ? $post->post_type : null;
  111. }
  112. return $post_type;
  113. }
  114. /**
  115. * Check and Map grid element shortcodes if required.
  116. * @since 4.5
  117. */
  118. function vc_grid_item_editor_shortcodes() {
  119. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/editor/class-vc-grid-item-editor.php' );
  120. // TODO: remove this because mapping can be based on post_type
  121. if ( ( 'true' === vc_request_param( 'vc_grid_item_editor' ) || ( is_admin() && vc_grid_item_get_post_type() === Vc_Grid_Item_Editor::postType() ) && vc_user_access()
  122. ->wpAny( 'edit_posts', 'edit_pages' )->part( 'grid_builder' )->can()->get() ) ) {
  123. global $vc_grid_item_editor;
  124. add_action( 'vc_user_access_check-shortcode_edit', array(
  125. &$vc_grid_item_editor,
  126. 'accessCheckShortcodeEdit',
  127. ), 10, 2 );
  128. add_action( 'vc_user_access_check-shortcode_all', array(
  129. &$vc_grid_item_editor,
  130. 'accessCheckShortcodeAll',
  131. ), 10, 2 );
  132. vc_grid_item_map_shortcodes();
  133. }
  134. }
  135. /**
  136. * add action in admin for vc grid item editor manager
  137. */
  138. add_action( 'init', 'vc_grid_item_editor_create_post_type' );
  139. add_action( 'admin_init', 'vc_grid_item_editor_init' );
  140. add_action( 'vc_after_init', 'vc_grid_item_editor_shortcodes' );
  141. /**
  142. * Call preview as ajax request is called.
  143. */
  144. add_action( 'wp_ajax_vc_gitem_preview', 'vc_grid_item_render_preview', 5 );
  145. /**
  146. * Add WP ui pointers in grid element editor.
  147. */
  148. if ( is_admin() ) {
  149. add_filter( 'vc_ui-pointers-vc_grid_item', 'vc_grid_item_register_pointer' );
  150. }
  151. /**
  152. * @param $pointers
  153. * @return mixed
  154. */
  155. function vc_grid_item_register_pointer( $pointers ) {
  156. $screen = get_current_screen();
  157. if ( 'add' === $screen->action ) {
  158. $pointers['vc_grid_item'] = array(
  159. 'name' => 'vcPointersController',
  160. 'messages' => array(
  161. array(
  162. 'target' => '#vc_templates-editor-button',
  163. 'options' => array(
  164. 'content' => sprintf( '<h3> %s </h3> <p> %s </p>', esc_html__( 'Start Here!', 'js_composer' ), esc_html__( 'Start easy - use predefined template as a starting point and modify it.', 'js_composer' ) ),
  165. 'position' => array(
  166. 'edge' => 'left',
  167. 'align' => 'center',
  168. ),
  169. ),
  170. ),
  171. array(
  172. 'target' => '[data-vc-navbar-control="animation"]',
  173. 'options' => array(
  174. 'content' => sprintf( '<h3> %s </h3> <p> %s </p>', esc_html__( 'Use Animations', 'js_composer' ), esc_html__( 'Select animation preset for grid element. "Hover" state will be added next to the "Normal" state tab.', 'js_composer' ) ),
  175. 'position' => array(
  176. 'edge' => 'right',
  177. 'align' => 'center',
  178. ),
  179. ),
  180. ),
  181. array(
  182. 'target' => '.vc_gitem_animated_block-shortcode',
  183. 'options' => array(
  184. 'content' => sprintf( '<h3> %s </h3> <p> %s </p>', esc_html__( 'Style Design Options', 'js_composer' ), esc_html__( 'Edit "Normal" state to set "Featured image" as a background, control zone sizing proportions and other design options (Height mode: Select "Original" to scale image without cropping).', 'js_composer' ) ),
  185. 'position' => array(
  186. 'edge' => 'bottom',
  187. 'align' => 'center',
  188. ),
  189. ),
  190. ),
  191. array(
  192. 'target' => '[data-vc-gitem="add-c"][data-vc-position="top"]',
  193. 'options' => array(
  194. 'content' => sprintf( '<h3> %s </h3> <p> %s </p>', esc_html__( 'Extend Element', 'js_composer' ), esc_html__( 'Additional content zone can be added to grid element edges (Note: This zone can not be animated).', 'js_composer' ) ) . '<p><img src="' . esc_url( vc_asset_url( 'vc/gb_additional_content.png' ) ) . '" alt="" /></p>',
  195. 'position' => array(
  196. 'edge' => 'right',
  197. 'align' => 'center',
  198. ),
  199. ),
  200. ),
  201. array(
  202. 'target' => '#wpadminbar',
  203. 'options' => array(
  204. 'content' => sprintf( '<h3> %s </h3> %s', esc_html__( 'Watch Video Tutorial', 'js_composer' ), '<p>' . esc_html__( 'Have a look how easy it is to work with grid element builder.', 'js_composer' ) . '</p>' . '<iframe width="500" height="281" src="https://www.youtube.com/embed/sBvEiIL6Blo" frameborder="0" allowfullscreen></iframe>' ),
  205. 'position' => array(
  206. 'edge' => 'top',
  207. 'align' => 'center',
  208. ),
  209. 'pointerClass' => 'vc_gitem-animated-block-pointer-video',
  210. 'pointerWidth' => '530',
  211. ),
  212. ),
  213. ),
  214. );
  215. }
  216. return $pointers;
  217. }
  218. /**
  219. * @return array|mixed|void
  220. */
  221. function vc_gitem_content_shortcodes() {
  222. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/class-vc-grid-item.php' );
  223. $grid_item = new Vc_Grid_Item();
  224. $invalid_shortcodes = apply_filters( 'vc_gitem_zone_grid_item_not_content_shortcodes', array(
  225. 'vc_gitem',
  226. 'vc_gitem_animated_block',
  227. 'vc_gitem_zone',
  228. 'vc_gitem_zone_a',
  229. 'vc_gitem_zone_b',
  230. 'vc_gitem_zone_c',
  231. 'vc_gitem_row',
  232. 'vc_gitem_col',
  233. ) );
  234. return array_diff( array_keys( $grid_item->shortcodes() ), $invalid_shortcodes );
  235. }
  236. /**
  237. * @param $content
  238. * @return false|int
  239. */
  240. function vc_gitem_has_content( $content ) {
  241. $tags = vc_gitem_content_shortcodes();
  242. $regexp = vc_get_shortcode_regex( implode( '|', $tags ) );
  243. return preg_match( '/' . $regexp . '/', $content );
  244. }
  245. /**
  246. * Add sub page to WPBakery Page Builder pages
  247. *
  248. * @since 4.5
  249. */
  250. function vc_gitem_add_submenu_page() {
  251. if ( vc_user_access()->part( 'grid_builder' )->can()->get() ) {
  252. $labels = Vc_Grid_Item_Editor::getPostTypesLabels();
  253. add_submenu_page( VC_PAGE_MAIN_SLUG, $labels['name'], $labels['name'], 'edit_posts', 'edit.php?post_type=' . rawurlencode( Vc_Grid_Item_Editor::postType() ), '' );
  254. }
  255. }
  256. /**
  257. * Highlight Vc submenu.
  258. * @since 4.5
  259. */
  260. function vc_gitem_menu_highlight() {
  261. global $parent_file, $submenu_file, $post_type;
  262. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/editor/class-vc-grid-item-editor.php' );
  263. if ( Vc_Grid_Item_Editor::postType() === $post_type && defined( 'VC_PAGE_MAIN_SLUG' ) ) {
  264. $parent_file = VC_PAGE_MAIN_SLUG;
  265. $submenu_file = 'edit.php?post_type=' . rawurlencode( Vc_Grid_Item_Editor::postType() );
  266. }
  267. }
  268. add_action( 'admin_head', 'vc_gitem_menu_highlight' );
  269. function vc_gitem_set_mapper_check_access() {
  270. if ( vc_user_access()->checkAdminNonce()->wpAny( 'edit_posts', 'edit_pages' )->part( 'grid_builder' )->can()->get() && 'true' === vc_post_param( 'vc_grid_item_editor' ) ) {
  271. vc_mapper()->setCheckForAccess( false );
  272. }
  273. }
  274. add_action( 'wp_ajax_vc_edit_form', 'vc_gitem_set_mapper_check_access' );