Skip to content

Commit

Permalink
Move blocks API endpoints to their own PHP file (#2505)
Browse files Browse the repository at this point in the history
This allows us to clean up the functions.php file a bit
  • Loading branch information
mleray authored and comzeradd committed Jan 22, 2025
1 parent 35956cb commit 49ec460
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 165 deletions.
4 changes: 0 additions & 4 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,8 @@ function planet4_get_option(string $key = '', $default = null)
'rest_api_init',
function (): void {
Rest::register_endpoints();
Api\Gallery::register_endpoint();
Api\Search::register_endpoint();
Api\Settings::register_endpoint();
Api\Covers::register_endpoint();
Api\Articles::register_endpoint();
Api\SocialMedia::register_endpoint();
}
);

Expand Down
37 changes: 0 additions & 37 deletions src/Api/Articles.php

This file was deleted.

37 changes: 0 additions & 37 deletions src/Api/Covers.php

This file was deleted.

42 changes: 0 additions & 42 deletions src/Api/Gallery.php

This file was deleted.

43 changes: 0 additions & 43 deletions src/Api/SocialMedia.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Blocks/Articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace P4\MasterTheme\Blocks;

use WP_REST_Server;

/**
* Class Articles
*
Expand Down Expand Up @@ -108,6 +110,7 @@ public function __construct()

add_action('enqueue_block_editor_assets', [ self::class, 'enqueue_editor_assets' ]);
add_action('wp_enqueue_scripts', [ self::class, 'enqueue_frontend_assets' ]);
add_action('rest_api_init', [ self::class, 'register_endpoint' ]);
}

/**
Expand Down Expand Up @@ -416,4 +419,29 @@ function ($tag) {

return $args;
}

/**
* Register endpoint to retrieve the articles for the Articles block.
*
* @example GET /wp-json/planet4/v1/get-posts/
*/
public static function register_endpoint(): void
{
register_rest_route(
self::REST_NAMESPACE,
'get-posts',
[
[
'permission_callback' => static function () {
return true;
},
'methods' => WP_REST_Server::READABLE,
'callback' => static function ($request) {
$covers = self::get_posts($request->get_params());
return rest_ensure_response($covers);
},
],
]
);
}
}
27 changes: 27 additions & 0 deletions src/Blocks/Covers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace P4\MasterTheme\Blocks;

use WP_REST_Server;
use P4\MasterTheme\ActionPage;

/**
Expand Down Expand Up @@ -157,6 +158,7 @@ public function __construct()

add_action('enqueue_block_editor_assets', [ self::class, 'enqueue_editor_assets' ]);
add_action('wp_enqueue_scripts', [ self::class, 'enqueue_frontend_assets' ]);
add_action('rest_api_init', [ self::class, 'register_endpoint' ]);
}

/**
Expand Down Expand Up @@ -489,4 +491,29 @@ private static function numberposts(string $layout): int

return self::POSTS_LIMIT;
}

/**
* Register endpoint to retrieve the covers for the Covers block.
*
* @example GET /wp-json/planet4/v1/get-covers/
*/
public static function register_endpoint(): void
{
register_rest_route(
self::REST_NAMESPACE,
'get-covers',
[
[
'permission_callback' => static function () {
return true;
},
'methods' => WP_REST_Server::READABLE,
'callback' => static function ($request) {
$covers = self::get_covers($request->get_params());
return rest_ensure_response($covers);
},
],
]
);
}
}
27 changes: 27 additions & 0 deletions src/Blocks/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace P4\MasterTheme\Blocks;

use WP_Block_Type_Registry;
use WP_REST_Server;

class Gallery extends BaseBlock
{
Expand Down Expand Up @@ -100,6 +101,7 @@ public function register_gallery_block(): void

add_action('enqueue_block_editor_assets', [ self::class, 'enqueue_editor_assets' ]);
add_action('wp_enqueue_scripts', [ self::class, 'enqueue_frontend_assets' ]);
add_action('rest_api_init', [ self::class, 'register_endpoint' ]);
}

/**
Expand Down Expand Up @@ -198,4 +200,29 @@ public static function get_images(array $fields): array

return $images;
}

/**
* Endpoint to retrieve the images for the Gallery block
*
* @example GET /wp-json/planet4/v1/gallery/images/
*/
public static function register_endpoint(): void
{
register_rest_route(
self::REST_NAMESPACE,
'gallery/images',
[
[
'permission_callback' => static function () {
return true;
},
'methods' => WP_REST_Server::READABLE,
'callback' => static function ($request) {
$images = self::get_images($request->get_params());
return rest_ensure_response($images);
},
],
]
);
}
}
6 changes: 4 additions & 2 deletions src/Blocks/HappyPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct()
]
);

add_action('rest_api_init', [ self::class, 'register_endpoints' ]);
add_action('rest_api_init', [ self::class, 'register_endpoint' ]);
}

/**
Expand Down Expand Up @@ -114,8 +114,10 @@ public static function get_data(object $fields): array

/**
* Endpoint to retrieve the data for the Happy Point block
*
* @example GET /wp-json/planet4/v1/get-happypoint-data
*/
public static function register_endpoints(): void
public static function register_endpoint(): void
{
register_rest_route(
self::REST_NAMESPACE,
Expand Down
29 changes: 29 additions & 0 deletions src/Blocks/SocialMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace P4\MasterTheme\Blocks;

use WP_REST_Server;

/**
* Class SocialMedia
* @package P4\MasterTheme\Blocks
Expand Down Expand Up @@ -96,6 +98,7 @@ public function register_socialmedia_block(): void

add_action('enqueue_block_editor_assets', [ self::class, 'enqueue_editor_assets' ]);
add_action('wp_enqueue_scripts', [ self::class, 'enqueue_frontend_assets' ]);
add_action('rest_api_init', [ self::class, 'register_endpoint' ]);
}

/**
Expand Down Expand Up @@ -234,5 +237,31 @@ private static function get_fb_oembed_url(string $url, string $provider): string

return $url;
}

/**
* Endpoint to get the code for Instagram embeds in the Social Media block.
*
* @example GET /wp-json/planet4/v1/get-instagram-embed
*/
public static function register_endpoint(): void
{
register_rest_route(
self::REST_NAMESPACE,
'/get-instagram-embed',
[
[
'permission_callback' => static function () {
return true;
},
'methods' => WP_REST_Server::READABLE,
'callback' => static function ($fields) {
$url = $fields['url'] ?? '';
$embed_code = self::get_fb_oembed_html($url, 'instagram');
return rest_ensure_response($embed_code);
},
],
]
);
}
}
// phpcs:enable Generic.Files.LineLength.MaxExceeded

0 comments on commit 49ec460

Please sign in to comment.