Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mega Menu Block #29

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions mega-menu/classes/class-wpcomsp-blocks-self-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Plugin Autoupdate Filter Self Update class.
* sets up autoupdates for this GitHub-hosted plugin.
*
* @package wpcomsp
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

class WPCOMSP_Blocks_Self_Update {

public static $instance;

/**
* Get instance of this class.
*
* @return WPCOMSP_Blocks_Self_Update
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Initialize WordPress hooks
*/
public function hooks() {
add_filter( 'update_plugins_opsoasis.wpspecialprojects.com', array( $this, 'self_update' ), 10, 3 );
}

/**
* Check for updates to this plugin
*
* @param array $update Array of update data.
* @param array $plugin_data Array of plugin data.
* @param string $plugin_file Path to plugin file.
*
* @return array|bool Array of update data or false if no update available.
*/
public function self_update( $update, array $plugin_data, string $plugin_file ) {
// Already completed update check elsewhere.
if ( ! empty( $update ) ) {
return $update;
}

$plugin_filename_parts = explode( '/', $plugin_file );

// Ask opsoasis.mystagingwebsite.com if there's an update.
$response = wp_remote_get(
'https://opsoasis.wpspecialprojects.com/wp-json/opsoasis-blocks-version-manager/v1/update-check',
array(
'body' => array(
'plugin' => $plugin_filename_parts[0],
'version' => $plugin_data['Version'],
),
)
);

// Bail if this plugin wasn't found on opsoasis.mystagingwebsite.com.
if ( 404 === wp_remote_retrieve_response_code( $response ) || 202 === wp_remote_retrieve_response_code( $response ) ) {
return $update;
}

$updated_version = wp_remote_retrieve_body( $response );
$updated_array = json_decode( $updated_version, true );

return array(
'slug' => $updated_array['slug'],
'version' => $updated_array['version'],
'url' => $updated_array['package_url'],
'package' => $updated_array['package_url'],
);
}
}
88 changes: 88 additions & 0 deletions mega-menu/mega-menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Plugin Name: Mega Menu
* Description: Add a menu item that opens a template part area to display as a mega menu.
* Version: 0.1.0
* Requires at least: 6.6
* Requires PHP: 7.2
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: mega-menu
*
* @package a8csp
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

// If no other WPCOMSP Block Plugin added the self update class, add it.
if ( ! class_exists( 'WPCOMSP_Blocks_Self_Update' ) ) {
require __DIR__ . '/classes/class-wpcomsp-blocks-self-update.php';

$wpcomsp_blocks_self_update = WPCOMSP_Blocks_Self_Update::get_instance();
$wpcomsp_blocks_self_update->hooks();
}

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*
* @return void
*/
function a8csp_mega_menu_block_init() {
register_block_type_from_metadata( __DIR__ . '/build' );
}
add_action( 'init', 'a8csp_mega_menu_block_init' );

/**
* Adds a custom template part area for mega menus to the list of template part areas.
*
* @param array $areas Existing array of template part areas.
*
* @return array Modified array of template part areas.
*/
function a8csp_mega_menu_template_areas( array $areas ) {
$mega_menu_area = apply_filters(
'a8csp_mega_menu_area_args',
array(
'area' => 'menu',
'area_tag' => 'div',
'description' => __( 'Menu templates are used to create sections of a mega menu.', 'a8csp' ),
'icon' => '',
'label' => __( 'Mega Menu', 'a8csp' ),
)
);

$areas[] = $mega_menu_area;

return $areas;
}

add_filter( 'default_wp_template_part_areas', 'a8csp_mega_menu_template_areas' );

/**
* Add Mega Menu block to the list of allowed blocks for the Navigation block.
*
* @param array $args The block type registration arguments.
* @param string $block_type The block type name including namespace.
*
* @return array
*/
function a8csp_mega_menu_navigation_filter( $args, $block_type ) {
if ( 'core/navigation' === $block_type ) {
if ( is_array( $args['allowed_blocks'] ) ) {
$updated_args = array_push( $args['allowed_blocks'], 'a8csp/mega-menu' );

$args['allowedBlocks'] = $updated_args;
}
}

return $args;
}

add_filter( 'register_block_type_args', 'a8csp_mega_menu_navigation_filter', 10, 2 );
Loading