<?php

/**
 * Handles admin bar logic for the theme builder.
 *
 * @since 1.0.1
 */
final class FLThemeBuilderAdminBar {

	/**
	 * Initialize admin bar logic.
	 *
	 * @since 1.0.1
	 * @return void
	 */
	static public function init() {
		add_action( 'wp', __CLASS__ . '::setup' );
	}

	/**
	 * Setup hooks.
	 *
	 * @since 1.0.1
	 * @return void
	 */
	static public function setup() {

		if ( is_admin() || ! is_admin_bar_showing() ) {
			return;
		}
		if ( ! FLBuilderUserAccess::current_user_can( 'theme_builder_editing' ) ) {
			return;
		}
		if ( 'fl-theme-layout' == get_post_type() ) {
			return;
		}

		add_action( 'wp_enqueue_scripts', __CLASS__ . '::enqueue_scripts' );
		add_action( 'admin_bar_menu',     __CLASS__ . '::add_builder_dropdown', 1000 );
	}

	/**
	 * Enqueues admin bar styles and scripts.
	 *
	 * @since 1.0.1
	 * @return void
	 */
	static public function enqueue_scripts() {

		$layouts = FLThemeBuilderLayoutData::get_current_page_layouts();

		if ( count( $layouts ) ) {
			wp_enqueue_style( 'fl-theme-builder-admin-bar', FL_THEME_BUILDER_URL . 'css/fl-theme-builder-admin-bar.css', array(), FL_THEME_BUILDER_VERSION );
		}
	}

	/**
	 * Adds the drop down menu for the Page Builder
	 * admin link if the current location has layouts.
	 *
	 * @since 1.0.1
	 * @param object $bar An instance of the WordPress admin bar.
	 * @return void
	 */
	static public function add_builder_dropdown( $bar ) {

		global $wp_the_query;

		$layouts = FLThemeBuilderLayoutData::get_current_page_layouts();
		$parent  = $bar->get_node( 'fl-builder-frontend-edit-link' );

		if ( count( $layouts ) ) {

			if ( ! $parent ) {
				$bar->add_node( array(
					'id'    => 'fl-builder-frontend-edit-link',
					'title' => '<span class="ab-icon"></span>' . FLBuilderModel::get_branding(),
					'href'  => '',
				));
			} else {

				$post_type = get_post_type_object( $wp_the_query->post->post_type );
				$title     = '<span class="fl-builder-ab-title">' . get_the_title( $wp_the_query->post->ID ) . '</span>';
				$tag       = '<span class="fl-builder-ab-tag">' . $post_type->labels->singular_name . '</span>';

				$bar->add_node( array(
					'parent' => 'fl-builder-frontend-edit-link',
					'id'     => 'fl-theme-builder-frontend-edit-link',
					'title'  => $title . $tag,
					'href'   => FLBuilderModel::get_edit_url( $wp_the_query->post->ID ),
				));
			}
		}

		foreach ( $layouts as $type => $data ) {

			if ( ! is_array( $data ) ) {
				continue;
			}

			foreach ( $data as $layout ) {

				$title  = '<span class="fl-builder-ab-title">' . get_the_title( $layout['id'] ) . '</span>';
				$tag    = '<span class="fl-builder-ab-tag">' . ucwords( $type ) . '</span>';

				$bar->add_node( array(
					'parent' => 'fl-builder-frontend-edit-link',
					'id'     => 'fl-theme-builder-edit-link-' . $layout['id'],
					'title'  => $title . $tag,
					'href'   => FLBuilderModel::get_edit_url( $layout['id'] ),
				));

				if ( 'part' !== $type ) {
					break;
				}
			}
		}
	}
}

FLThemeBuilderAdminBar::init();
