class-fl-builder-user-templates.php 18.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
<?php

/**
 * User defined templates for the builder.
 *
 * @since 1.8
 */
final class FLBuilderUserTemplates {

	/**
	 * Initialize hooks.
	 *
	 * @since 1.8
	 * @return void
	 */
	static public function init()
	{
		/* Actions */
		add_action( 'plugins_loaded',                                  __CLASS__ . '::init_ajax' );
		add_action( 'init',                                            __CLASS__ . '::load_settings', 1 );
		add_action( 'init',                                            __CLASS__ . '::register_post_type' );
		add_action( 'wp_footer',                                       __CLASS__ . '::render_ui_js_templates' );
		add_action( 'fl_builder_ui_panel_after_modules',               __CLASS__ . '::render_ui_panel_node_templates' );
		add_action( 'fl_builder_template_selector_content',            __CLASS__ . '::render_selector_content' );
		
		/* Filters */
		add_filter( 'template_include',                                __CLASS__ . '::template_include', 999 );
		add_filter( 'fl_builder_has_templates',                        __CLASS__ . '::has_templates', 10, 2 );
		add_filter( 'fl_builder_template_selector_data',               __CLASS__ . '::selector_data', 999 );
		add_filter( 'fl_builder_template_selector_filter_data',        __CLASS__ . '::selector_filter_data' );
		add_filter( 'fl_builder_ui_bar_title',                         __CLASS__ . '::ui_bar_title' );
		add_filter( 'fl_builder_ui_bar_buttons',                       __CLASS__ . '::ui_bar_buttons' );
		add_filter( 'fl_builder_ui_js_config',                         __CLASS__ . '::ui_js_config' );
		add_filter( 'fl_builder_settings_form_config',                 __CLASS__ . '::settings_form_config' );
		add_filter( 'fl_builder_content_classes',                      __CLASS__ . '::content_classes' );
		add_filter( 'fl_builder_render_nodes',                         __CLASS__ . '::render_nodes' );
		add_filter( 'fl_builder_row_attributes',                       __CLASS__ . '::row_attributes', 10, 2 );
		add_filter( 'fl_builder_column_attributes',                    __CLASS__ . '::column_attributes', 10, 2 );
		add_filter( 'fl_builder_module_attributes',                    __CLASS__ . '::module_attributes', 10, 2 );
	}

	/**
	 * Initialize AJAX actions.
	 *
	 * @since 1.8
	 * @return void
	 */
	static public function init_ajax()
	{
		FLBuilderAJAX::add_action( 'render_user_template_settings',    __CLASS__ . '::render_settings' );
		FLBuilderAJAX::add_action( 'render_node_template_settings',    __CLASS__ . '::render_node_settings', array( 'node_id' ) );
		FLBuilderAJAX::add_action( 'save_user_template',               'FLBuilderModel::save_user_template', array( 'settings' ) );
		FLBuilderAJAX::add_action( 'delete_user_template',             'FLBuilderModel::delete_user_template', array( 'template_id' ) );
		FLBuilderAJAX::add_action( 'save_node_template',               'FLBuilderModel::save_node_template', array( 'node_id', 'settings' ) );
		FLBuilderAJAX::add_action( 'delete_node_template',             'FLBuilderModel::delete_node_template', array( 'template_id' ) );
	}

	/**
	 * Loads files for the template settings. 
	 *
	 * @since 1.8
	 * @return void
	 */
	static public function load_settings()
	{
		require_once FL_BUILDER_USER_TEMPLATES_DIR . 'includes/user-template-settings.php';
		require_once FL_BUILDER_USER_TEMPLATES_DIR . 'includes/node-template-settings.php';
	}

	/**
	 * Registers the custom post type for user templates.
	 *
	 * @since 1.1.3
	 * @since 1.5.7 Added template category taxonomy.
	 * @return void
	 */
	static public function register_post_type()
	{
		// Vars for checking if the templates admin should be public.
		$admin_enabled 		= FLBuilderModel::user_templates_admin_enabled();
		$can_edit_global 	= current_user_can( FLBuilderModel::get_global_templates_editing_capability() );
		$can_edit 			= FLBuilderModel::current_user_has_editing_capability();

		// Register the template post type.
		register_post_type('fl-builder-template', apply_filters( 'fl_builder_register_template_post_type_args', array(
			'public'            => $admin_enabled && $can_edit ? true : false,
			'labels'            => array(
				'name'               => _x( 'Templates', 'Custom post type label.', 'fl-builder' ),
				'singular_name'      => _x( 'Template', 'Custom post type label.', 'fl-builder' ),
				'menu_name'          => _x( 'Templates', 'Custom post type label.', 'fl-builder' ),
				'name_admin_bar'     => _x( 'Template', 'Custom post type label.', 'fl-builder' ),
				'add_new'            => _x( 'Add New', 'Custom post type label.', 'fl-builder' ),
				'add_new_item'       => _x( 'Add New Template', 'Custom post type label.', 'fl-builder' ),
				'new_item'           => _x( 'New Template', 'Custom post type label.', 'fl-builder' ),
				'edit_item'          => _x( 'Edit Template', 'Custom post type label.', 'fl-builder' ),
				'view_item'          => _x( 'View Template', 'Custom post type label.', 'fl-builder' ),
				'all_items'          => _x( 'All Templates', 'Custom post type label.', 'fl-builder' ),
				'search_items'       => _x( 'Search Templates', 'Custom post type label.', 'fl-builder' ),
				'parent_item_colon'  => _x( 'Parent Templates:', 'Custom post type label.', 'fl-builder' ),
				'not_found'          => _x( 'No templates found.', 'Custom post type label.', 'fl-builder' ),
				'not_found_in_trash' => _x( 'No templates found in Trash.', 'Custom post type label.', 'fl-builder' )
			),
			'menu_icon'			=> 'dashicons-welcome-widgets-menus',
			'supports'          => array(
				'title',
				'revisions',
				'page-attributes'
			),
			'taxonomies'		=> array(
				'fl-builder-template-category'
			),
			'publicly_queryable' 	=> $can_edit || $can_edit_global,
			'exclude_from_search'	=> true
		) ) );
		
		// Register the template category tax.
		register_taxonomy( 'fl-builder-template-category', array( 'fl-builder-template' ), apply_filters( 'fl_builder_register_template_category_args', array(
			'labels'            => array(
				'name'              => _x( 'Template Categories', 'Custom taxonomy label.', 'fl-builder' ),
				'singular_name'     => _x( 'Template Category', 'Custom taxonomy label.', 'fl-builder' ),
				'search_items'      => _x( 'Search Template Categories', 'Custom taxonomy label.', 'fl-builder' ),
				'all_items'         => _x( 'All Template Categories', 'Custom taxonomy label.', 'fl-builder' ),
				'parent_item'       => _x( 'Parent Template Category', 'Custom taxonomy label.', 'fl-builder' ),
				'parent_item_colon' => _x( 'Parent Template Category:', 'Custom taxonomy label.', 'fl-builder' ),
				'edit_item'         => _x( 'Edit Template Category', 'Custom taxonomy label.', 'fl-builder' ),
				'update_item'       => _x( 'Update Template Category', 'Custom taxonomy label.', 'fl-builder' ),
				'add_new_item'      => _x( 'Add New Template Category', 'Custom taxonomy label.', 'fl-builder' ),
				'new_item_name'     => _x( 'New Template Category Name', 'Custom taxonomy label.', 'fl-builder' ),
				'menu_name'         => _x( 'Categories', 'Custom taxonomy label.', 'fl-builder' ),
			),
			'hierarchical'      => true,
			'public'            => true,
			'show_admin_column' => true
		) ) );
		
		// Register the template type tax.
		register_taxonomy( 'fl-builder-template-type', array( 'fl-builder-template' ), apply_filters( 'fl_builder_register_template_type_args', array(
			'label'             => _x( 'Type', 'Custom taxonomy label.', 'fl-builder' ),
			'hierarchical'      => false,
			'public'            => false,
			'show_admin_column' => true
		) ) );
	}

	/**
	 * Trys to load page.php for editing a builder template.
	 *
	 * @since 1.0
	 * @param string $template The current template to be loaded.
	 * @return string
	 */
	static public function template_include( $template )
	{
		global $post;
		
		if ( 'string' == gettype( $template ) && $post && $post->post_type == 'fl-builder-template' ) {

			$page = locate_template( array( 'page.php' ) );

			if ( ! empty( $page ) ) {
				return $page;
			}
		}

		return $template;
	}

	/**
	 * Hook into the fl_builder_has_templates filter and always return true
	 * so the template selector shows even if there are no core templates
	 * or third party theme templates available.
	 *
	 * @since 1.8
	 * @param bool $has_templates
	 * @return bool
	 */
	static public function has_templates( $has_templates )
	{
		$enabled_templates = FLBuilderModel::get_enabled_templates();
		
		if ( 'core' == $enabled_templates )  {
			$templates = FLBuilderModel::get_template_selector_data();
			return ( count( $templates['templates'] ) > 0 );
		}
		else if ( 'user' == $enabled_templates )  {
			return true;
		}
		else if ( 'enabled' == $enabled_templates )  {
			return true;
		}
		else if ( 'disabled' == $enabled_templates )  {
			return false;
		}
		
		return true;
	}

	/**
	 * Disables core or third party templates if all templates are disabled
	 * or only user templates are enabled.
	 *
	 * @since 1.8
	 * @param array $data
	 * @return array
	 */
	static public function selector_data( $data )
	{
		if ( in_array( FLBuilderModel::get_enabled_templates(), array( 'user', 'disabled' ) ) )  {
			$data = array(
				'templates'   => array(),
				'categorized' => array()
			);
		}
		
		return $data;
	}

	/**
	 * Returns data needed for the template selector's category filter.
	 *
	 * @since 1.8
	 * @param array $data
	 * @return array
	 */
	static public function selector_filter_data( $data )
	{
		$enabled_templates = FLBuilderModel::get_enabled_templates();
		
		if ( 'user' == $enabled_templates )  {
			$data = array( 'yours' => __( 'Your Templates', 'fl-builder' ) );
		}
		else if ( 'enabled' == $enabled_templates )  {
			$data['yours'] = __( 'Your Templates', 'fl-builder' );
		}
		else if ( 'disabled' == $enabled_templates )  {
			$data = array();
		}
		
		return $data;
	}

	/**
	 * Renders user defined templates in the template selector.
	 *
	 * @since 1.8
	 * @return void
	 */
	static public function render_selector_content()
	{
		if ( in_array( FLBuilderModel::get_enabled_templates(), array( 'user', 'enabled' ) ) )  {
			
			$user_templates = FLBuilderModel::get_user_templates();
			
			require_once FL_BUILDER_USER_TEMPLATES_DIR . 'includes/template-selector.php';
		}
	}

	/**
	 * Sets the UI bar title to the current template name.
	 *
	 * @since 1.8
	 * @param string $title
	 * @return string
	 */
	static public function ui_bar_title( $title )
	{
		global $wp_the_query;
		
		if( FLBuilderModel::is_post_user_template() ) {
			$title = sprintf( __( 'Row/Module: %s', 'fl-builder' ), get_the_title( $wp_the_query->post->ID ) );
		}
		
		return $title;
	}

	/**
	 * Modifies the UI bar buttons for user templates if needed.
	 *
	 * @since 1.8
	 * @param array $buttons
	 * @return array
	 */
	static public function ui_bar_buttons( $buttons )
	{
		$is_template        = FLBuilderModel::is_post_user_template();
		$is_module_template = FLBuilderModel::is_post_user_template( 'module' );
		$enabled_templates  = FLBuilderModel::get_enabled_templates();
		
		if ( isset( $buttons['tools'] ) && $is_module_template ) {
			$buttons['tools']['show'] = false;
		}
		if ( isset( $buttons['templates'] ) && ( $is_template || 'disabled' == $enabled_templates ) ) {
			$buttons['templates']['show'] = false;
		}
		if ( isset( $buttons['add-content'] ) && $is_module_template ) {
			$buttons['add-content']['show'] = false;
		}
		
		return $buttons;
	}

	/**
	 * Sets the JS config variables for user templates.
	 *
	 * @since 1.8
	 * @param array $config
	 * @return array
	 */
	static public function ui_js_config( $config )
	{
		return array_merge( $config, array(
			'enabledTemplates'              => FLBuilderModel::get_enabled_templates(),
			'isUserTemplate'                => FLBuilderModel::is_post_user_template() ? true : false,
			'userCanEditGlobalTemplates'    => current_user_can( FLBuilderModel::get_global_templates_editing_capability() ) ? true : false,
			'userTemplateType'              => FLBuilderModel::get_user_template_type()
		) );
	}

	/**
	 * Renders the UI panel for node templates.
	 *
	 * @since 1.6.3
	 * @return void
	 */
	static public function render_ui_panel_node_templates()
	{
		if ( FLBuilderModel::node_templates_enabled() ) {
			
			$saved_rows    = FLBuilderModel::get_node_templates( 'row' );
			$saved_modules = FLBuilderModel::get_node_templates( 'module' );
			$node_template = FLBuilderModel::is_post_node_template();
			
			// Don't show global rows for node templates.
			foreach ( $saved_rows as $key => $val ) {
				if ( $node_template && $val['global'] ) {
					unset( $saved_rows[ $key ] );
				}
			}
			
			// Don't show global modules for node templates.
			foreach ( $saved_modules as $key => $val ) {
				if ( $node_template && $val['global'] ) {
					unset( $saved_modules[ $key ] );
				}
			}
			
			include FL_BUILDER_USER_TEMPLATES_DIR . 'includes/ui-panel-node-templates.php';
		}
	}

	/**
	 * Renders the markup for the JavaScript UI templates. 
	 *
	 * @since 1.8
	 * @return void
	 */
	static public function render_ui_js_templates()
	{
		if ( FLBuilderModel::is_builder_active() ) {
			include FL_BUILDER_USER_TEMPLATES_DIR . 'includes/ui-js-templates.php';
		}
	}

	/**
	 * Modifies the config of settings forms for user templates if needed.
	 *
	 * @since 1.8
	 * @param array $config
	 * @return array
	 */
	static public function settings_form_config( $config )
	{
		$is_row    = stristr( $config['class'], 'fl-builder-row-settings' );
		$is_col    = stristr( $config['class'], 'fl-builder-col-settings' );
		$is_module = stristr( $config['class'], 'fl-builder-module-settings' );
		
		if ( $is_row || $is_col || $is_module ) {
		
			$post_data = FLBuilderModel::get_post_data();
			$global    = false;
			
			if ( isset( $post_data['node_id'] ) ) {
				$global = FLBuilderModel::is_node_global( FLBuilderModel::get_node( $post_data['node_id'] ) );
			}
			else if ( isset( $post_data['template_id'] ) ) {
				$template_post_id = FLBuilderModel::get_node_template_post_id( $post_data['template_id'] );
				$global = ! $template_post_id ? false : FLBuilderModel::is_post_global_node_template( $template_post_id );
			}
			
			if ( $global ) {
				$config['badges']['global'] = _x( 'Global', 'Indicator for global node templates.', 'fl-builder' );
			}
			if ( ( $is_row || $is_module ) && ! $global && ! FLBuilderModel::is_post_node_template() && FLBuilderModel::node_templates_enabled() ) {
				$config['buttons'][] = 'save-as';
			}
		}
		
		return $config;
	}

	/**
	 * Renders the settings form for saving a user defined template.
	 *
	 * @since 1.0
	 * @return array
	 */
	static public function render_settings()
	{
		$defaults = FLBuilderModel::get_settings_form_defaults( 'user_template' );
		$form     = FLBuilderModel::get_settings_form( 'user_template' );

		return FLBuilder::render_settings(array(
			'class'   => 'fl-builder-user-template-settings',
			'title'   => $form['title'],
			'tabs'    => $form['tabs']
		), $defaults);
	}

	/**
	 * Renders the settings form for saving a node template.
	 *
	 * @since 1.6.3
	 * @param string $node_id The node whose template settings to load.
	 * @return array
	 */
	static public function render_node_settings( $node_id = null )
	{
		$defaults 	= FLBuilderModel::get_settings_form_defaults( 'node_template' );
		$form     	= FLBuilderModel::get_settings_form( 'node_template' );
		$node 		= FLBuilderModel::get_node( $node_id );

		return FLBuilder::render_settings(array(
			'class'   => 'fl-builder-node-template-settings',
			'attrs'   => 'data-node="'. $node->node .'"',
			'title'   => str_replace( '%s', ucwords( $node->type ), $form['title'] ),
			'tabs'    => $form['tabs']
		), $defaults);
	}

	/**
	 * Adds template classes to the builder's content classes.
	 *
	 * @since 1.8
	 * @param string $classes
	 * @return string
	 */
	static public function content_classes( $classes )
	{
		// Add template classes to the content class.
		if ( FLBuilderModel::is_post_user_template() ) {
			$classes .= ' fl-builder-template';
			$classes .= ' fl-builder-' . FLBuilderModel::get_user_template_type() . '-template';
		}
		
		// Add the global templates locked class.
		if ( ! current_user_can( FLBuilderModel::get_global_templates_editing_capability() ) ) {
			$classes .= ' fl-builder-global-templates-locked';
		}

		return $classes;
	}

	/**
	 * Short circuits node rendering and renders modules if this
	 * is a module template.
	 *
	 * @since 1.8
	 * @param bool $render
	 * @return bool
	 */
	static public function render_nodes( $render )
	{
		if ( FLBuilderModel::is_post_user_template( 'module' ) ) {
			FLBuilder::render_modules();
			return false;
		}
		
		return $render;
	}

	/**
	 * Adds template specific attributes for rows.
	 *
	 * @since 1.8
	 * @param array $attrs
	 * @param object $row
	 * @return array
	 */
	static public function row_attributes( $attrs, $row )
	{
		$global = FLBuilderModel::is_node_global( $row );
		$active = FLBuilderModel::is_builder_active();
		
		if ( $global && $active ) {
			$attrs['class'][] = 'fl-node-global';
		}
		if ( $global && $active ) {
			$attrs['data-template'] = $row->template_id;
			$attrs['data-template-node'] = $row->template_node_id;
			$attrs['data-template-url'] = FLBuilderModel::get_node_template_edit_url( $row->template_id );
		}
		
		return $attrs;
	}

	/**
	 * Adds template specific attributes for columns.
	 *
	 * @since 1.8
	 * @param array $attrs
	 * @param object $col
	 * @return array
	 */
	static public function column_attributes( $attrs, $col )
	{
		$global = FLBuilderModel::is_node_global( $col );
		$active = FLBuilderModel::is_builder_active();
		
		if ( $global && $active ) {
			$attrs['class'][] = 'fl-node-global';
		}
		if ( $global && $active ) {
			$attrs['data-template'] = $col->template_id;
			$attrs['data-template-node'] = $col->template_node_id;
		}
		
		return $attrs;
	}

	/**
	 * Adds template specific attributes for modules.
	 *
	 * @since 1.8
	 * @param array $attrs
	 * @param object $module
	 * @return array
	 */
	static public function module_attributes( $attrs, $module )
	{
		$global = FLBuilderModel::is_node_global( $module );
		$active = FLBuilderModel::is_builder_active();
		
		if ( $global && $active ) {
			$attrs['class'][] = 'fl-node-global';
		}
		if ( $global && $active ) {
			$attrs['data-template'] = $module->template_id;
			$attrs['data-template-node'] = $module->template_node_id;
		}
		
		return $attrs;
	}
}

FLBuilderUserTemplates::init();