| 1 |
<?php |
| 2 |
/** |
| 3 |
* The icon library |
| 4 |
* |
| 5 |
* @package BoldBlocks |
| 6 |
* @author Phi Phan <mrphipv@gmail.com> |
| 7 |
* @copyright Copyright (c) 2022, Phi Phan |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace BoldBlocks; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
if ( ! class_exists( IconLibrary::class ) ) : |
| 16 |
/** |
| 17 |
* The controller class for icon library. |
| 18 |
*/ |
| 19 |
class IconLibrary extends CoreComponent { |
| 20 |
/** |
| 21 |
* Run main hooks |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function run() { |
| 26 |
// Add rest api endpoint to query icon library. |
| 27 |
add_action( 'rest_api_init', [ $this, 'register_icon_library_endpoint' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Build a custom endpoint to query icon library. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function register_icon_library_endpoint() { |
| 36 |
register_rest_route( |
| 37 |
'cbb/v1', |
| 38 |
'/getIconLibrary/', |
| 39 |
array( |
| 40 |
'methods' => 'GET', |
| 41 |
'callback' => [ $this, 'get_icon_library' ], |
| 42 |
'permission_callback' => function () { |
| 43 |
return current_user_can( 'publish_posts' ); |
| 44 |
}, |
| 45 |
) |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get icon library. |
| 51 |
* |
| 52 |
* @param WP_REST_Request $request The request object. |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function get_icon_library( $request ) { |
| 56 |
// icons file path. |
| 57 |
$icons_file = $this->the_plugin_instance->get_file_path( 'data/icon-library/icons.json' ); |
| 58 |
|
| 59 |
// Send the error if the icons file is not exists. |
| 60 |
if ( ! \file_exists( $icons_file ) ) { |
| 61 |
wp_send_json_error( __( 'The icons.json file is not exists.', 'content-blocks-builder' ), 500 ); |
| 62 |
} |
| 63 |
|
| 64 |
// Parse json. |
| 65 |
$icons = wp_json_file_decode( $icons_file, [ 'associative' => true ] ); |
| 66 |
|
| 67 |
// Query svg images from the media library. |
| 68 |
$media_svg_images = $this->query_svg_images(); |
| 69 |
|
| 70 |
if ( $media_svg_images ) { |
| 71 |
$icons = $media_svg_images + $icons; |
| 72 |
} |
| 73 |
|
| 74 |
wp_send_json( |
| 75 |
[ |
| 76 |
'data' => $icons, |
| 77 |
'success' => true, |
| 78 |
] |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Query SVG images from the library |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
private function query_svg_images() { |
| 88 |
$media_svgs = []; |
| 89 |
$images = get_posts( |
| 90 |
[ |
| 91 |
'post_type' => 'attachment', |
| 92 |
'post_mime_type' => [ 'image/svg+xml' ], |
| 93 |
'post_status' => 'any', |
| 94 |
'posts_per_page' => 100, |
| 95 |
] |
| 96 |
); |
| 97 |
|
| 98 |
if ( $images ) { |
| 99 |
foreach ( $images as $image ) { |
| 100 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 101 |
$icon = file_get_contents( get_attached_file( $image->ID ) ); |
| 102 |
if ( $icon ) { |
| 103 |
$media_svgs[] = [ |
| 104 |
'name' => $image->post_name, |
| 105 |
'title' => $image->post_title, |
| 106 |
'icon' => $icon, |
| 107 |
'categories' => [ 'Media Library' ], |
| 108 |
'provider' => 'Media Library', |
| 109 |
]; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $media_svgs; |
| 115 |
} |
| 116 |
} |
| 117 |
endif; |
| 118 |
|