Astra_Ext_Model::add_admin_menu( $admin_bar )

Add Admin menu item


Description


Parameters

(object) (Required) WP_Admin_Bar $admin_bar Admin bar.


Return

(void)


Source

File: classes/class-astra-ext-model.php

	public function add_admin_menu( $admin_bar ) {
		if ( is_admin() ) {
			return;
		}

		// Check if current user can have edit access.
		if ( ! current_user_can( 'edit_posts' ) ) {
			return;
		}

		$custom_layout_addon_active = Astra_Ext_Extension::is_active( 'advanced-hooks' ) ? true : false;
		$page_headers_addon_active  = Astra_Ext_Extension::is_active( 'advanced-headers' ) ? true : false;
		$post_id                    = get_the_ID() ? get_the_ID() : 0;
		$current_post               = $post_id ? get_post( $post_id, OBJECT ) : false;
		$has_shortcode              = ( is_object( $current_post ) && has_shortcode( $current_post->post_content, 'astra_custom_layout' ) ) ? true : false;

		if ( $custom_layout_addon_active || $page_headers_addon_active || $has_shortcode ) {
			$custom_layouts = false;
			if ( $custom_layout_addon_active ) {
				$option = array(
					'location'  => 'ast-advanced-hook-location',
					'exclusion' => 'ast-advanced-hook-exclusion',
					'users'     => 'ast-advanced-hook-users',
				);

				$custom_layouts = Astra_Target_Rules_Fields::get_instance()->get_posts_by_conditions( ASTRA_ADVANCED_HOOKS_POST_TYPE, $option );
			}

			$page_headers = false;
			if ( $page_headers_addon_active ) {
				$option = array(
					'location'  => 'ast-advanced-headers-location',
					'exclusion' => 'ast-advanced-headers-exclusion',
					'users'     => 'ast-advanced-headers-users',
					'page_meta' => 'adv-header-id-meta',
				);

				$page_headers = Astra_Target_Rules_Fields::get_instance()->get_posts_by_conditions( 'astra_adv_header', $option );
			}

			if ( $custom_layouts || $page_headers || $has_shortcode ) {
				$admin_bar->add_node(
					array(
						'id'    => 'astra-advanced-layouts',
						'title' => '<span class="ab-item astra-admin-logo"></span>',
					)
				);
			}

			if ( is_array( $custom_layouts ) && ! empty( $custom_layouts ) ) {
				$admin_bar->add_group(
					array(
						'id'     => 'ast_custom_layouts_group',
						'parent' => 'astra-advanced-layouts',
					)
				);
				$admin_bar->add_node(
					array(
						'id'     => 'custom-layout-title',
						'parent' => 'ast_custom_layouts_group',
						'title'  => esc_html__( 'Edit Custom Layout', 'astra-addon' ),
					)
				);

				// Add dynamic layouts assigned on current location.
				foreach ( $custom_layouts as $post_id => $post_data ) {
					$post_type = get_post_type();
					if ( ASTRA_ADVANCED_HOOKS_POST_TYPE != $post_type ) {
						$layout_title = get_the_title( $post_id );
						$admin_bar->add_node(
							array(
								'id'     => 'edit-custom-layout-' . esc_attr( $post_id ),
								'href'   => esc_url( get_edit_post_link( $post_id ) ),
								'title'  => esc_attr( $layout_title ),
								'parent' => 'ast_custom_layouts_group',
							)
						);
					}
				}
			}

			if ( is_array( $page_headers ) && ! empty( $page_headers ) ) {
				$admin_bar->add_group(
					array(
						'id'     => 'ast_page_headers_group',
						'parent' => 'astra-advanced-layouts',
					)
				);
				$admin_bar->add_node(
					array(
						'id'     => 'page-headers-title',
						'parent' => 'ast_page_headers_group',
						'title'  => esc_html__( 'Edit Page Header', 'astra-addon' ),
					)
				);

				// Add dynamic headers assigned on current location.
				foreach ( $page_headers as $post_id => $post_data ) {
					$post_type = get_post_type();
					if ( 'astra_adv_header' != $post_type ) {
						$layout_title = get_the_title( $post_id );
						$admin_bar->add_node(
							array(
								'id'     => 'edit-page-header-' . esc_attr( $post_id ),
								'href'   => esc_url( get_edit_post_link( $post_id ) ),
								'title'  => esc_attr( $layout_title ),
								'parent' => 'ast_page_headers_group',
							)
						);
					}
				}
			}

			if ( $has_shortcode ) {
				$pattern = get_shortcode_regex( array( 'astra_custom_layout' ) );

				if ( preg_match_all( '/' . $pattern . '/s', $current_post->post_content, $matches ) ) {
					$output = array();

					foreach ( $matches[0] as $key => $value ) {
						$as_string = str_replace( ' ', '&', trim( $matches[3][ $key ] ) ); // $matches[3] return the shortcode attribute as string & replace space with '&' for parse_str() function.
						$as_string = str_replace( '"', '', $as_string );
						parse_str( $as_string, $sub_attrs );

						$output[] = $sub_attrs; // Get all shortcode attribute keys.
					}

					if ( ! empty( $output ) ) {
						$admin_bar->add_group(
							array(
								'id'     => 'ast_cl_shortcode_group',
								'parent' => 'astra-advanced-layouts',
							)
						);
						$admin_bar->add_node(
							array(
								'id'     => 'cl-shortcode-title',
								'parent' => 'ast_cl_shortcode_group',
								'title'  => esc_html__( 'Edit Shortcode Layouts', 'astra-addon' ),
							)
						);
						foreach ( $output as $key => $value ) {
							foreach ( $value as $attr_key => $attr_val ) {
								$cl_layout_id = absint( $attr_val );
								$layout_title = get_the_title( $cl_layout_id );
								$admin_bar->add_node(
									array(
										'id'     => 'edit-cl-shortcode-layout-' . esc_attr( $cl_layout_id ),
										'href'   => esc_url( get_edit_post_link( $cl_layout_id ) ),
										'title'  => esc_attr( $layout_title ),
										'parent' => 'ast_cl_shortcode_group',
									)
								);
							}
						}
					}
				}
			}
		}
	}

Changelog

Changelog
Version Description
4.0.0 Introduced.


User Contributed Notes

You must log in before being able to contribute a note or feedback.