balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-templates.php (4955B)


      1 <?php // phpcs:ignore WordPress.Files.FileName
      2 /**
      3  * Templates overrides for pages.
      4  *
      5  * @since 4.0.0
      6  * @package Redux Framework
      7  */
      8 
      9 namespace ReduxTemplates;
     10 
     11 if ( ! defined( 'ABSPATH' ) ) {
     12 	exit; // Exit if accessed directly.
     13 }
     14 
     15 /**
     16  * Redux Templates Templates Class
     17  *
     18  * @since 4.0.0
     19  */
     20 class Templates {
     21 
     22 	/**
     23 	 * Default container width.
     24 	 *
     25 	 * @var int
     26 	 */
     27 	public static $content_width = 1200;
     28 
     29 	/**
     30 	 * ReduxTemplates Template.
     31 	 *
     32 	 * @since 4.0.0
     33 	 */
     34 	public function __construct() {
     35 		global $pagenow;
     36 
     37 		if ( 'widgets.php' === $pagenow ) {
     38 			return;
     39 		}
     40 
     41 		if ( ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) && \Redux_Enable_Gutenberg::$is_disabled ) {
     42 
     43 				// We don't want to add templates unless it's a gutenberg page.
     44 				return;
     45 		}
     46 
     47 		// Include ReduxTemplates default template without wrapper.
     48 		add_filter( 'template_include', array( $this, 'template_include' ) );
     49 
     50 		// Override the default content-width when using Redux templates so the template doesn't look like crao.
     51 		add_action( 'wp', array( $this, 'modify_template_content_width' ) );
     52 
     53 		// Add ReduxTemplates supported Post types in page template.
     54 		$post_types = get_post_types( array(), 'object' );
     55 
     56 		if ( ! empty( $post_types ) ) {
     57 			foreach ( $post_types as $post_type ) {
     58 				if ( isset( $post_type->name ) && isset( $post_type->show_in_rest ) && true === $post_type->show_in_rest ) {
     59 					add_filter( "theme_{$post_type->name}_templates", array( $this, 'add_templates' ) );
     60 				}
     61 			}
     62 		}
     63 
     64 		add_filter( 'admin_body_class', array( $this, 'add_body_class' ), 999 );
     65 
     66 	}
     67 
     68 	/**
     69 	 * Add the redux-template class to the admin body if a redux-templates page type is selected.
     70 	 *
     71 	 * @param string|null $classes Classes string for admin panel.
     72 	 *
     73 	 * @return string|null
     74 	 * @since 4.1.19
     75 	 */
     76 	public function add_body_class( ?string $classes ): ?string {
     77 		global $post;
     78 
     79 		$screen = get_current_screen();
     80 
     81 		if ( 'post' === $screen->base && get_current_screen()->is_block_editor() ) {
     82 			$check = get_post_meta( $post->ID, '_wp_page_template', true );
     83 			if ( strpos( $check, 'redux-templates_' ) !== false ) {
     84 				$classes .= ' redux-template';
     85 			}
     86 		}
     87 
     88 		return $classes;
     89 	}
     90 
     91 	/**
     92 	 * Override the $content_width variable for themes so our templates work properly and don't look squished.
     93 	 *
     94 	 * @param array $to_find Template keys to check against.
     95 	 *
     96 	 * @since 4.0.0
     97 	 * @return bool
     98 	 */
     99 	public function check_template( $to_find = array() ) {
    100 		global $post;
    101 		if ( ! empty( $post ) ) {
    102 			$template = get_page_template_slug( $post->ID );
    103 			if ( false !== strpos( $template, 'redux' ) ) {
    104 				$test = mb_strtolower( preg_replace( '/[^A-Za-z0-9 ]/', '', $template ) );
    105 				foreach ( $to_find as $key ) {
    106 					if ( false !== strpos( $test, $key ) ) {
    107 						return true;
    108 					}
    109 				}
    110 			}
    111 		}
    112 		return false;
    113 	}
    114 
    115 	/**
    116 	 * Override the $content_width variable for themes so our templates work properly and don't look squished.
    117 	 *
    118 	 * @since 4.0.0
    119 	 */
    120 	public function modify_template_content_width() {
    121 		$to_find = array( 'cover', 'canvas', 'fullwidth' );
    122 		if ( $this->check_template( $to_find ) ) {
    123 			global $content_width;
    124 			if ( $content_width < 1000 ) {
    125 				$content_width = get_option( '_redux_content_width', self::$content_width );
    126 			}
    127 		}
    128 	}
    129 
    130 	/**
    131 	 * Override the $content_width variable for themes so our templates work properly and don't look squished.
    132 	 *
    133 	 * @since 4.0.0
    134 	 */
    135 	public static function inline_editor_css() {
    136 		global $content_width;
    137 		if ( $content_width < 1000 ) {
    138 			$content_width = get_option( '_redux_content_width', self::$content_width );
    139 			return ".redux-template .wp-block {max-width: {$content_width}px;}";
    140 		}
    141 	}
    142 
    143 	/**
    144 	 * Include the template
    145 	 *
    146 	 * @param string $template Template type.
    147 	 *
    148 	 * @return string
    149 	 * @since 4.0.0
    150 	 */
    151 	public function template_include( $template ) {
    152 		if ( is_singular() ) {
    153 			$page_template = get_post_meta( get_the_ID(), '_wp_page_template', true );
    154 			if ( 'redux-templates_full_width' === $page_template ) {
    155 				$template = REDUXTEMPLATES_DIR_PATH . 'classes/templates/template-full-width.php';
    156 			} elseif ( 'redux-templates_contained' === $page_template ) {
    157 				$template = REDUXTEMPLATES_DIR_PATH . 'classes/templates/template-contained.php';
    158 			} elseif ( 'redux-templates_canvas' === $page_template ) {
    159 				$template = REDUXTEMPLATES_DIR_PATH . 'classes/templates/template-canvas.php';
    160 			}
    161 		}
    162 
    163 		return $template;
    164 	}
    165 
    166 	/**
    167 	 * Hook to add the templates to the dropdown
    168 	 *
    169 	 * @param array $post_templates Default post templates array.
    170 	 *
    171 	 * @return array
    172 	 * @since 4.0.0
    173 	 */
    174 	public function add_templates( $post_templates ) {
    175 		$post_templates['redux-templates_contained']  = __( 'Redux Contained', 'redux-framework' );
    176 		$post_templates['redux-templates_full_width'] = __( 'Redux Full Width', 'redux-framework' );
    177 		$post_templates['redux-templates_canvas']     = __( 'Redux Canvas', 'redux-framework' );
    178 
    179 		return $post_templates;
    180 	}
    181 }