/Site/wp-content/plugins/js_composer/include/autoload/post-type-default-template.php

https://gitlab.com/rubengrb/ws.vidrialum · PHP · 208 lines · 131 code · 29 blank · 48 comment · 19 complexity · 820423c80ef6f7308ddf6371a4068c38 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Return true value for filter 'wpb_vc_js_status_filter'.
  7. * It allows to start backend editor on load.
  8. * @since 4.12
  9. *
  10. * @return string
  11. */
  12. function vc_set_default_content_for_post_type_wpb_vc_js_status_filter() {
  13. return 'true';
  14. }
  15. /**
  16. * Set default content by post type in editor.
  17. *
  18. * Data for post type templates stored in settings.
  19. *
  20. * @param $post_content
  21. * @param $post
  22. * @since 4.12
  23. *
  24. * @return null
  25. */
  26. function vc_set_default_content_for_post_type( $post_content, $post ) {
  27. if ( ! empty( $post_content ) || ! vc_backend_editor()->isValidPostType( $post->post_type ) ) {
  28. return $post_content;
  29. }
  30. $template_settings = new Vc_Setting_Post_Type_Default_Template_Field( 'general', 'default_template_post_type' );
  31. if ( ( $new_post_content = $template_settings->getTemplateByPostType( $post->post_type ) ) !== null ) {
  32. add_filter( 'wpb_vc_js_status_filter', 'vc_set_default_content_for_post_type_wpb_vc_js_status_filter' );
  33. return $new_post_content;
  34. }
  35. return $post_content;
  36. }
  37. /**
  38. * Default template for post types manager
  39. *
  40. * Class Vc_Setting_Post_Type_Default_Template_Field
  41. *
  42. * @since 4.12
  43. */
  44. Class Vc_Setting_Post_Type_Default_Template_Field {
  45. protected $tab;
  46. protected $key;
  47. protected $post_types = false;
  48. function __construct( $tab, $key ) {
  49. $this->tab = $tab;
  50. $this->key = $key;
  51. add_action( 'vc_settings_tab-general', array(
  52. $this,
  53. 'addField',
  54. ) );
  55. }
  56. protected function getFieldName() {
  57. return __( 'Default template for post types', 'js_composer' );
  58. }
  59. protected function getFieldKey() {
  60. require_once vc_path_dir( 'SETTINGS_DIR', 'class-vc-settings.php' );
  61. return Vc_Settings::getFieldPrefix() . $this->key;
  62. }
  63. protected function isValidPostType( $type ) {
  64. return post_type_exists( $type );
  65. }
  66. protected function getPostTypes() {
  67. if ( false === $this->post_types ) {
  68. require_once vc_path_dir( 'SETTINGS_DIR', 'class-vc-roles.php' );
  69. $vc_roles = new Vc_Roles();
  70. $this->post_types = $vc_roles->getPostTypes();
  71. }
  72. return $this->post_types;
  73. }
  74. protected function getTemplates() {
  75. return $this->getTemplatesEditor()->getAllTemplates();
  76. }
  77. protected function getTemplatesEditor() {
  78. return visual_composer()->templatesPanelEditor();
  79. }
  80. /**
  81. * Get settings data for default templates
  82. *
  83. * @return array|mixed|void
  84. */
  85. protected function get() {
  86. require_once vc_path_dir( 'SETTINGS_DIR', 'class-vc-settings.php' );
  87. return ( $value = Vc_Settings::get( $this->key ) ) ? $value : array();
  88. }
  89. /**
  90. * Get template's shortcodes string
  91. *
  92. * @param $template_data
  93. * @return string|null
  94. */
  95. protected function getTemplate( $template_data ) {
  96. $template = null;
  97. $template_settings = preg_split( '/\:\:/', $template_data );
  98. $template_id = $template_settings[1];
  99. $template_type = $template_settings[0];
  100. if ( ! isset( $template_id, $template_type ) || '' === $template_id || '' === $template_type ) {
  101. return $template;
  102. }
  103. WPBMap::addAllMappedShortcodes();
  104. if ( 'my_templates' === $template_type ) {
  105. $saved_templates = get_option( $this->getTemplatesEditor()
  106. ->getOptionName() );
  107. $content = trim( $saved_templates[ $template_id ]['template'] );
  108. $content = str_replace( '\"', '"', $content );
  109. $pattern = get_shortcode_regex();
  110. $template = preg_replace_callback( "/{$pattern}/s", 'vc_convert_shortcode', $content );
  111. } else {
  112. if ( 'default_templates' === $template_type ) {
  113. $template_data = $this->getTemplatesEditor()
  114. ->getDefaultTemplate( $template_id );
  115. if ( isset( $template_data['content'] ) ) {
  116. $template = $template_data['content'];
  117. }
  118. } else {
  119. $template_preview = apply_filters( 'vc_templates_render_backend_template_preview', $template_id, $template_type );
  120. if ( (string) $template_preview !== (string) $template_id ) {
  121. $template = $template_preview;
  122. }
  123. }
  124. }
  125. return $template;
  126. }
  127. public function getTemplateByPostType( $type ) {
  128. $value = $this->get();
  129. return isset( $value[ $type ] ) ? $this->getTemplate( $value[ $type ] ) : null;
  130. }
  131. public function sanitize( $settings ) {
  132. foreach ( $settings as $type => $template ) {
  133. if ( empty( $template ) ) {
  134. unset( $settings[ $type ] );
  135. } else if ( ! $this->isValidPostType( $type ) || ! $this->getTemplate( $template ) ) {
  136. add_settings_error( $this->getFieldKey(), 1, __( 'Invalid template or post type.', 'js_composer' ), 'error' );
  137. return $settings;
  138. }
  139. }
  140. return $settings;
  141. }
  142. public function render() {
  143. vc_include_template( 'pages/vc-settings/default-template-post-type.tpl.php', array(
  144. 'post_types' => $this->getPostTypes(),
  145. 'templates' => $this->getTemplates(),
  146. 'title' => $this->getFieldName(),
  147. 'value' => $this->get(),
  148. 'field_key' => $this->getFieldKey(),
  149. ) );
  150. }
  151. /**
  152. * Add field settings page
  153. *
  154. * Method called by vc hook vc_settings_tab-general.
  155. */
  156. public function addField() {
  157. vc_settings()->addField( $this->tab, $this->getFieldName(), $this->key, array(
  158. $this,
  159. 'sanitize',
  160. ), array(
  161. $this,
  162. 'render',
  163. ) );
  164. }
  165. }
  166. /**
  167. * Start only for admin part with hooks
  168. */
  169. if ( is_admin() ) {
  170. /**
  171. * Initialize Vc_Setting_Post_Type_Default_Template_Field
  172. * Called by admin_init hook
  173. */
  174. function vc_settings_post_type_default_template_field_init() {
  175. new Vc_Setting_Post_Type_Default_Template_Field( 'general', 'default_template_post_type' );
  176. }
  177. add_filter( 'default_content', 'vc_set_default_content_for_post_type', 100, 2 );
  178. add_action( 'admin_init', 'vc_settings_post_type_default_template_field_init', 8 );
  179. }