PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/bridge/plugins/js_composer/js_composer/include/autoload/hook-vc-grid.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 255 lines | 162 code | 22 blank | 71 comment | 14 complexity | 5e5b5ccd10dba38586d7dc1e308090da MD5 | raw file
  1. <?php
  2. /**
  3. * Class Vc_Hooks_Vc_Grid
  4. * @since 4.4
  5. */
  6. Class Vc_Hooks_Vc_Grid implements Vc_Vendor_Interface {
  7. protected $grid_id_unique_name = 'vc_gid'; // if you change this also change in vc-basic-grid.php
  8. /**
  9. * Initializing hooks for grid element,
  10. * Add actions to save appended shortcodes to post meta (for rendering in preview with shortcode id)
  11. * And add action to hook request for grid data, to output it.
  12. * @since 4.4
  13. */
  14. public function load() {
  15. // Hook for set post settings meta with shortcodes data
  16. /**
  17. * Filter called after page saved, used to append some post meta for post
  18. * Used to save appended grid shortcodes to post meta with serialized array,
  19. * @see Vc_Hooks_Vc_Grid::gridSavePostSettings (fetching shortcodes and return it data) and Vc_Hooks_Vc_Grid::get_shortcode_regex (search exact shortcode in content)
  20. * @see Vc_Post_Admin::setSettings for filter info
  21. * @deprecated since 4.4.3 due to invalid hash algorithm on saving.
  22. */
  23. add_filter( 'vc_hooks_vc_post_settings', array(
  24. &$this,
  25. 'gridSavePostSettings'
  26. ), 10, 3 );
  27. /**
  28. * @since 4.4.3
  29. */
  30. add_filter( 'vc_hooks_vc_post_settings', array(
  31. &$this,
  32. 'gridSavePostSettingsId'
  33. ), 10, 3 );
  34. /**
  35. * Used to output shortcode data for ajax request. called on any page request.
  36. */
  37. // add_action( 'template_redirect', array( &$this, 'getGridDataForAjax' ) );
  38. add_action( 'wp_ajax_vc_get_vc_grid_data', array( &$this, 'getGridDataForAjax' ) );
  39. add_action( 'wp_ajax_nopriv_vc_get_vc_grid_data', array( &$this, 'getGridDataForAjax' ) );
  40. }
  41. /**
  42. * @since 4.4.3
  43. * @deprecated and should not be used and will be removed in future!
  44. * @return string
  45. */
  46. private function getShortcodeRegexForHash() {
  47. $tagnames = apply_filters( 'vc_grid_shortcodes_tags', array(
  48. 'vc_basic_grid',
  49. 'vc_masonry_grid',
  50. 'vc_media_grid',
  51. 'vc_masonry_media_grid'
  52. ) ); // return only grid shortcodes
  53. $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) );
  54. // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
  55. // Also, see shortcode_unautop() and shortcode.js.
  56. return
  57. '\\[' // Opening bracket
  58. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
  59. . "($tagregexp)" // 2: Shortcode name
  60. . '(?![\\w-])' // Not followed by word character or hyphen
  61. . '(' // 3: Unroll the loop: Inside the opening shortcode tag
  62. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  63. . '(?:'
  64. . '\\/(?!\\])' // A forward slash not followed by a closing bracket
  65. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  66. . ')*?'
  67. . ')'
  68. . '(?:'
  69. . '(\\/)' // 4: Self closing tag ...
  70. . '\\]' // ... and closing bracket
  71. . '|'
  72. . '\\]' // Closing bracket
  73. . '(?:'
  74. . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
  75. . '[^\\[]*+' // Not an opening bracket
  76. . '(?:'
  77. . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
  78. . '[^\\[]*+' // Not an opening bracket
  79. . ')*+'
  80. . ')'
  81. . '\\[\\/\\2\\]' // Closing shortcode tag
  82. . ')?'
  83. . ')'
  84. . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
  85. }
  86. /**
  87. * @since 4.4.3
  88. * @return string
  89. */
  90. private function getShortcodeRegexForId() {
  91. return
  92. '\\[' // Opening bracket
  93. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
  94. . "([\\w-_]+)" // 2: Shortcode name
  95. . '(?![\\w-])' // Not followed by word character or hyphen
  96. . '(' // 3: Unroll the loop: Inside the opening shortcode tag
  97. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  98. . '(?:'
  99. . '\\/(?!\\])' // A forward slash not followed by a closing bracket
  100. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  101. . ')*?'
  102. . '(?:'
  103. . '('
  104. . $this->grid_id_unique_name // 4: GridId must exist
  105. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  106. . ')+'
  107. . ')'
  108. . ')'
  109. . '(?:'
  110. . '(\\/)' // 5: Self closing tag ...
  111. . '\\]' // ... and closing bracket
  112. . '|'
  113. . '\\]' // Closing bracket
  114. . '(?:'
  115. . '(' // 6: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
  116. . '[^\\[]*+' // Not an opening bracket
  117. . '(?:'
  118. . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
  119. . '[^\\[]*+' // Not an opening bracket
  120. . ')*+'
  121. . ')'
  122. . '\\[\\/\\2\\]' // Closing shortcode tag
  123. . ')?'
  124. . ')'
  125. . '(\\]?)'; // 7: Optional second closing brocket for escaping shortcodes: [[tag]]
  126. }
  127. /**
  128. * Set page meta box values with vc_adv_pager shortcodes data
  129. * @since 4.4
  130. * @deprecated since 4.4.3
  131. *
  132. * @param array $settings
  133. * @param $post_id
  134. * @param $post
  135. *
  136. * @return array - shortcode settings to save.
  137. */
  138. public function gridSavePostSettings( array $settings, $post_id, $post ) {
  139. $pattern = $this->getShortcodeRegexForHash();
  140. preg_match_all( "/$pattern/", $post->post_content, $found ); // fetch only needed shortcodes
  141. $settings['vc_grid'] = array();
  142. if ( is_array( $found ) && ! empty( $found[0] ) ) {
  143. $to_save = array();
  144. foreach ( $found[3] as $key => $shortcode_atts ) {
  145. if ( strpos( $shortcode_atts, 'vc_gid:' ) !== false ) {
  146. continue;
  147. }
  148. $atts = shortcode_parse_atts( $shortcode_atts );
  149. $data = array(
  150. 'tag' => $found[2][ $key ],
  151. 'atts' => $atts,
  152. 'content' => $found[5][ $key ],
  153. );
  154. $hash = sha1( serialize( $data ) );
  155. $to_save[ $hash ] = $data;
  156. }
  157. if ( ! empty( $to_save ) ) {
  158. $settings['vc_grid'] = array( 'shortcodes' => $to_save );
  159. }
  160. }
  161. return $settings;
  162. }
  163. /**
  164. * @since 4.4.3
  165. *
  166. * @param array $settings
  167. * @param $post_id
  168. * @param $post
  169. *
  170. * @return array
  171. */
  172. public function gridSavePostSettingsId( array $settings, $post_id, $post ) {
  173. $pattern = $this->getShortcodeRegexForId();
  174. preg_match_all( "/$pattern/", $post->post_content, $found ); // fetch only needed shortcodes
  175. $settings['vc_grid_id'] = array();
  176. if ( is_array( $found ) && ! empty( $found[0] ) ) {
  177. $to_save = array();
  178. foreach ( $found[1] as $key => $parse_able ) {
  179. if ( empty( $parse_able ) || $parse_able != '[' ) {
  180. $id_pattern = '/' . $this->grid_id_unique_name . '\:([\w-_]+)/';
  181. $id_value = $found[4][ $key ];
  182. preg_match( $id_pattern, $id_value, $id_matches );
  183. if ( ! empty( $id_matches ) ) {
  184. $id_to_save = $id_matches[1];
  185. // why we need to check if shortcode is parse able?
  186. // 1: if it is escaped it must not be displayed (parsed)
  187. // 2: so if 1 is true it must not be saved in database meta
  188. $shortcode_tag = $found[2][ $key ];
  189. $shortcode_atts_string = $found[3][ $key ];
  190. /** @var $atts array */
  191. $atts = shortcode_parse_atts( $shortcode_atts_string );
  192. $content = $found[6][ $key ];
  193. $data = array(
  194. 'tag' => $shortcode_tag,
  195. 'atts' => $atts,
  196. 'content' => $content,
  197. );
  198. $to_save[ $id_to_save ] = $data;
  199. }
  200. }
  201. }
  202. if ( ! empty( $to_save ) ) {
  203. $settings['vc_grid_id'] = array( 'shortcodes' => $to_save );
  204. }
  205. }
  206. return $settings;
  207. }
  208. /**
  209. * @since 4.4
  210. *
  211. * @output/@return string - grid data for ajax request.
  212. */
  213. public function getGridDataForAjax() {
  214. $tag = vc_request_param( 'tag' );
  215. if ( $tag ) {
  216. $shorcode_fishbone = visual_composer()->getShortCode( $tag );
  217. if ( is_object( $shorcode_fishbone ) ) {
  218. /** @var $vc_grid WPBakeryShortcode_Vc_Basic_Grid */
  219. $vc_grid = $shorcode_fishbone->shortcodeClass();
  220. if ( method_exists( $vc_grid, 'renderAjax' ) ) {
  221. die( $vc_grid->renderAjax( vc_request_param( 'data' ) ) );
  222. }
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * @since 4.4
  229. * @var Vc_Hooks_Vc_Grid $hook
  230. */
  231. $hook = new Vc_Hooks_Vc_Grid();
  232. // when visual composer initialized let's trigger Vc_Grid hooks.
  233. add_action( 'vc_after_init', array( $hook, 'load' ) );