PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/types/library/toolset/toolset-common/user-editors/medium/content-template.php

https://gitlab.com/Fraternal-Group/fraternal
PHP | 270 lines | 222 code | 42 blank | 6 comment | 34 complexity | 52491e83b4cf1ff08d809ac95323f088 MD5 | raw file
  1. <?php
  2. if( ! class_exists( 'Toolset_User_Editors_Medium_Abstract', false ) )
  3. require_once( TOOLSET_COMMON_PATH . '/user-editors/medium/abstract.php' );
  4. class Toolset_User_Editors_Medium_Content_Template
  5. extends Toolset_User_Editors_Medium_Abstract {
  6. protected $slug = 'view-template';
  7. protected $allowed_templates;
  8. protected $option_name_editor_choice = '_toolset_user_editors_editor_choice';
  9. public function __construct() {
  10. if( array_key_exists( 'ct_id', $_REQUEST ) )
  11. $this->id = $_REQUEST['ct_id'];
  12. if( $this->id && array_key_exists( 'ct_editor_choice', $_REQUEST ) )
  13. update_post_meta( $this->id, $this->option_name_editor_choice, sanitize_text_field( $_REQUEST['ct_editor_choice'] ) );
  14. add_filter( 'toolset_user_editors_backend_html_editor_select', array( $this, '_filterEditorSelection' ) );
  15. }
  16. public function userEditorChoice() {
  17. if( $this->user_editor_choice !== null )
  18. return $this->user_editor_choice;
  19. if( ! $this->getId() )
  20. return false;
  21. $content_template_id = wpv_getget( 'ct_id' );
  22. if( $editor_choice = get_post_meta( $content_template_id, $this->option_name_editor_choice, true ) ) {
  23. $this->user_editor_choice = $editor_choice;
  24. return $editor_choice;
  25. } // backward compatibility (since Views Visual Comopser Beta we used 'wpv_ct_editor_choice')
  26. elseif ( $editor_choice = get_post_meta( $content_template_id, 'wpv_ct_editor_choice', true ) ) {
  27. $this->user_editor_choice = $editor_choice;
  28. update_post_meta( $content_template_id, $this->option_name_editor_choice, $editor_choice );
  29. delete_post_meta( $content_template_id, 'wpv_ct_editor_choice' );
  30. return $editor_choice;
  31. } elseif( get_post_type( $content_template_id ) == $this->slug ) {
  32. update_post_meta( $content_template_id, $this->option_name_editor_choice, 'basic' );
  33. return 'basic';
  34. }
  35. return false;
  36. }
  37. public function getFrontendTemplates() {
  38. if( $this->allowed_templates !== null )
  39. return $this->allowed_templates;
  40. $content_template_usages = $this->getUsages();
  41. $theme_template_files = (array) wp_get_theme()->get_files( 'php', 1, true );
  42. $wpv_options_patterns = array(
  43. 'views_template_for_' => array(
  44. 'label' => __( 'Single page', 'wpv-views' ),
  45. 'domain' => 'post',
  46. 'template_hierarchy' => array(
  47. 'single-%NAME%.php',
  48. 'single.php',
  49. 'singular.php',
  50. 'index.php'
  51. )
  52. ),
  53. 'views_template_archive_for_' => array(
  54. 'label' => __( 'Post archive', 'wpv-views' ),
  55. 'domain' => 'post',
  56. 'template_hierarchy' => array(
  57. 'archive-%NAME%.php',
  58. 'archive.php',
  59. 'index.php'
  60. )
  61. ),
  62. 'views_template_loop_' => array(
  63. 'label' => __( 'Taxonomy archive', 'wpv-views' ),
  64. 'domain' => 'taxonomy',
  65. 'template_hierarchy' => array(
  66. 'taxonomy-%NAME%.php',
  67. 'taxonomy.php',
  68. 'archive.php',
  69. 'index.php'
  70. )
  71. ),
  72. 'view_loop_preview_post_type_' => array(
  73. 'label' => __( 'View loop', 'wpv-views' ),
  74. 'domain' => 'post',
  75. 'template_hierarchy' => array(
  76. 'single-%NAME%.php',
  77. 'single.php',
  78. 'singular.php',
  79. 'index.php'
  80. )
  81. ),
  82. 'view_wpa_loop_preview_post_type_' => array(
  83. 'label' => __( 'WordPress Archive loop', 'wpv-views' ),
  84. 'domain' => 'post',
  85. 'template_hierarchy' => array(
  86. 'archive-%NAME%.php',
  87. 'archive.php',
  88. 'index.php'
  89. )
  90. ),
  91. 'view_wpa_loop_preview_taxonomy_' => array(
  92. 'label' => __( 'WordPress Archive loop', 'wpv-views' ),
  93. 'domain' => 'taxonomy',
  94. 'template_hierarchy' => array(
  95. 'taxonomy-%NAME%.php',
  96. 'taxonomy.php',
  97. 'archive.php',
  98. 'index.php'
  99. )
  100. ),
  101. );
  102. $this->allowed_templates = array();
  103. foreach( $content_template_usages as $usage => $ct_id ) {
  104. foreach( $wpv_options_patterns as $pattern => $settings ) {
  105. if( strpos( $usage, $pattern ) !== false ) {
  106. $type_name = str_replace( $pattern, '', $usage );
  107. $type_object = $settings['domain'] == 'post'
  108. ? get_post_type_object( $type_name )
  109. : get_taxonomy( $type_name );
  110. foreach( $settings['template_hierarchy'] as $template_file ) {
  111. $template_file = str_replace( '%NAME%', $type_object->name, $template_file );
  112. if( array_key_exists( $template_file, $theme_template_files ) ) {
  113. $this->allowed_templates[] = array(
  114. 'slug' => $type_object->name,
  115. 'domain' => $settings['domain'],
  116. 'form-option-label' => $settings['label'] . ' ' . $type_object->labels->name,
  117. 'path' => $theme_template_files[ $template_file ]
  118. );
  119. break;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. // Make sure that the stored template path is in the allowed ones, or force it otherwise
  126. $allowed_paths = wp_list_pluck( $this->allowed_templates, 'path' );
  127. $current_template = get_post_meta( $_GET['ct_id'], $this->manager->getActiveEditor()->getOptionName(), true );
  128. if (
  129. isset( $_GET['ct_id'] )
  130. && ! empty( $allowed_paths )
  131. && (
  132. ! isset( $current_template['template_path'] )
  133. || ! in_array( $current_template['template_path'], $allowed_paths )
  134. )
  135. ) {
  136. $slide_allowed_template = array_slice( $this->allowed_templates, 0, 1 );
  137. $first_allowed_template = array_shift( $slide_allowed_template );
  138. $settings_to_store = array(
  139. 'template_path' => wp_slash( $first_allowed_template['path'] ),
  140. 'preview_domain' => $first_allowed_template['domain'],
  141. 'preview_slug' => $first_allowed_template['slug']
  142. );
  143. update_post_meta( $_GET['ct_id'], $this->manager->getActiveEditor()->getOptionName(), $settings_to_store );
  144. $stored = get_post_meta( $_GET['ct_id'], $this->manager->getActiveEditor()->getOptionName(), true );
  145. }
  146. return $this->allowed_templates;
  147. }
  148. private function getUsages() {
  149. $views_settings = WPV_Settings::get_instance();
  150. $views_options = $views_settings->get();
  151. $views_options = array_filter( $views_options, array( $this, 'filterTemplatesByTemplateId' ) );
  152. if ( isset( $_GET['ct_id'] ) ) {
  153. if (
  154. isset( $_GET['preview_post_type'] )
  155. && is_array( $_GET['preview_post_type'] )
  156. && ! empty ( $_GET['preview_post_type'] )
  157. ) {
  158. $preview_post_type = array_map( 'sanitize_text_field', $_GET['preview_post_type'] );
  159. foreach ( $preview_post_type as $prev_cpt ) {
  160. $views_options[ 'view_loop_preview_post_type_' . $prev_cpt ] = (int) $_GET['ct_id'];
  161. }
  162. }
  163. if (
  164. isset( $_GET['preview_post_type_archive'] )
  165. && is_array( $_GET['preview_post_type_archive'] )
  166. && ! empty ( $_GET['preview_post_type_archive'] )
  167. ) {
  168. $preview_post_type_archive = array_map( 'sanitize_text_field', $_GET['preview_post_type_archive'] );
  169. foreach ( $preview_post_type_archive as $prev_cpt ) {
  170. $views_options[ 'view_wpa_loop_preview_post_type_' . $prev_cpt ] = (int) $_GET['ct_id'];
  171. }
  172. }
  173. if (
  174. isset( $_GET['preview_taxonomy_archive'] )
  175. && is_array( $_GET['preview_taxonomy_archive'] )
  176. && ! empty ( $_GET['preview_taxonomy_archive'] )
  177. ) {
  178. $preview_taxonomy_archive = array_map( 'sanitize_text_field', $_GET['preview_taxonomy_archive'] );
  179. foreach ( $preview_taxonomy_archive as $prev_cpt ) {
  180. $views_options[ 'view_wpa_loop_preview_taxonomy_' . $prev_cpt ] = (int) $_GET['ct_id'];
  181. }
  182. }
  183. // @todo implement the rest of the Layout Loop usages
  184. }
  185. return $views_options;
  186. }
  187. private function filterTemplatesByTemplateId( $stored_value ) {
  188. if( ! isset( $_GET['ct_id'] ) )
  189. return false;
  190. return( $stored_value == $_GET['ct_id'] );
  191. }
  192. /**
  193. * @param $content_function callable
  194. */
  195. public function setHtmlEditorBackend( $content_function ) {
  196. add_filter( 'toolset_user_editors_backend_html_active_editor', $content_function );
  197. }
  198. public function _filterEditorSelection() {
  199. $control_editor_select = '';
  200. $editors = $this->manager->getEditors();
  201. if( count( $editors ) > 1 ) {
  202. $admin_url = admin_url( 'admin.php?page=ct-editor&ct_id='.$_GET['ct_id'] );
  203. $editor_current = '';
  204. $editor_switch_buttons = array();
  205. foreach( $editors as $editor ) {
  206. if ( $editor->getId() == $this->manager->getActiveEditor()->getId() ) {
  207. if ( 'basic' != $editor->getId() ) {
  208. $editor_current = sprintf( __( 'Using %1$s ', 'wpv-views' ), '<strong>' . $editor->getName() . '</strong>' );
  209. }
  210. } else {
  211. $editor_switch_buttons[] = '<a class="button" href="'.$admin_url.'&ct_editor_choice='.$editor->getId().'">'.sprintf( __( 'Design with %1$s', 'wpv-views' ), $editor->getName() ).'</a>';
  212. }
  213. }
  214. $control_editor_select .= '<div class="wpv-ct-control-switch-editor">';
  215. $control_editor_select .= $editor_current;
  216. //$control_editor_select .= __( 'Select Editor: ', 'wpv-views' );
  217. $control_editor_select .= join( ' ', array_reverse( $editor_switch_buttons ) );
  218. $control_editor_select .= '</div>';
  219. }
  220. return $control_editor_select;
  221. }
  222. public function pageReloadAfterBackendSave() {
  223. add_action( 'admin_print_footer_scripts', array( $this, '_actionPageReloadAfterBackendSave' ) );
  224. }
  225. public function _actionPageReloadAfterBackendSave() {
  226. echo "<script>jQuery( document ).on('ct_saved', function() { location.reload(); });</script>";
  227. }
  228. }