| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that sets up editer blocks for Charitable. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Admin |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Admin_Blocks' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Admin_Blocks |
| 21 |
* |
| 22 |
* @final |
| 23 |
* @since 1.8.0 |
| 24 |
*/ |
| 25 |
final class Charitable_Admin_Blocks { |
| 26 |
|
| 27 |
/** |
| 28 |
* The single instance of this class. |
| 29 |
* |
| 30 |
* @var Charitable_Admin|null |
| 31 |
*/ |
| 32 |
private static $instance = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Set up the class. |
| 36 |
* |
| 37 |
* @since 1.8.0 |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
|
| 41 |
if ( $this->allow_load() ) { |
| 42 |
|
| 43 |
add_action( 'init', array( $this, 'register_blocks' ) ); |
| 44 |
|
| 45 |
do_action( 'charitable_admin_blocks_loaded' ); |
| 46 |
|
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Indicate if current integration is allowed to load. |
| 52 |
* |
| 53 |
* @since 1.4.8 |
| 54 |
* |
| 55 |
* @return bool |
| 56 |
*/ |
| 57 |
public function allow_load() { |
| 58 |
|
| 59 |
return function_exists( 'register_block_type' ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Registers the block using the metadata loaded from the `block.json` file. |
| 64 |
* Behind the scenes, it registers also all assets so they can be enqueued |
| 65 |
* through the block editor in the corresponding context. |
| 66 |
* |
| 67 |
* @see https://developer.wordpress.org/reference/functions/register_block_type/ |
| 68 |
* |
| 69 |
* @since 1.8.0 |
| 70 |
*/ |
| 71 |
public function register_blocks() { |
| 72 |
if ( ! $this->is_block_registered( 'create-block/campaignblock' ) ) { |
| 73 |
register_block_type( |
| 74 |
charitable()->get_path( 'directory', true ) . 'assets/js/blocks/config/campaign-block.json', |
| 75 |
array( |
| 76 |
'render_callback' => array( $this, 'charitable_block_render_campaign_widget' ), |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private function is_block_registered( $block_name ) { |
| 83 |
// Use REST API to query if block exists by <namespace>/<name>. |
| 84 |
$route = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_name ); |
| 85 |
$request = rest_do_request( $route ); |
| 86 |
|
| 87 |
if ( $request->status == 404 ) { |
| 88 |
// Block is not found/registered. |
| 89 |
return false; |
| 90 |
} |
| 91 |
// Block is registered (status is 200). |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Renders the campaign widget block. |
| 97 |
* |
| 98 |
* @since 1.8.0 |
| 99 |
* |
| 100 |
* @param array $attributes The attributes for the block. |
| 101 |
*/ |
| 102 |
public function charitable_block_render_campaign_widget( $attributes ) { |
| 103 |
$campaign_id = $attributes['campaignID']; |
| 104 |
|
| 105 |
ob_start(); |
| 106 |
|
| 107 |
echo do_shortcode( '[campaign id="' . intval( $campaign_id ) . '"]' ); |
| 108 |
|
| 109 |
return ob_get_clean(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Loads admin-only scripts and stylesheets. |
| 114 |
* |
| 115 |
* @since 1.8.0 |
| 116 |
* |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
public function admin_enqueue_scripts() { |
| 120 |
|
| 121 |
$min = ''; // charitable_get_min_suffix(); // todo: undo this. |
| 122 |
$version = charitable()->get_version(); |
| 123 |
|
| 124 |
$assets_dir = charitable()->get_path( 'assets', false ); |
| 125 |
|
| 126 |
/* The following styles are only loaded on Charitable screens. */ |
| 127 |
$screen = get_current_screen(); |
| 128 |
|
| 129 |
// add data only for block editor pages, where the campaign block (among others) would be used. |
| 130 |
if ( method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) { |
| 131 |
|
| 132 |
wp_localize_script( |
| 133 |
'create-block-campaignblock-editor-script', |
| 134 |
'charitable_block_data', |
| 135 |
$this->get_localized_strings_block_data() |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
do_action( 'after_charitable_admin_block_enqueue_scripts', $min, $version, $assets_dir ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Retrieves localized strings for the Charitable Blocks editor. |
| 144 |
* |
| 145 |
* @since 1.8.0 |
| 146 |
* |
| 147 |
* @param array $args Optional. Arguments to customize the localized strings. |
| 148 |
* @return array An array of localized strings. |
| 149 |
*/ |
| 150 |
private function get_localized_strings_block_data( $args = array() ) { |
| 151 |
|
| 152 |
$defaults = array( |
| 153 |
'post_type' => array( Charitable::CAMPAIGN_POST_TYPE ), |
| 154 |
'posts_per_page' => -1, |
| 155 |
'post_status' => array( 'publish' ), |
| 156 |
); |
| 157 |
|
| 158 |
$args = wp_parse_args( $args, $defaults ); |
| 159 |
|
| 160 |
$query = new WP_Query( $args ); |
| 161 |
|
| 162 |
$campaigns_for_dropdown = array(); |
| 163 |
|
| 164 |
if ( ! empty( $query->posts ) ) : |
| 165 |
foreach ( $query->posts as $post_id => $campaign_post ) : |
| 166 |
$campaigns_for_dropdown[] = array( |
| 167 |
'label' => $campaign_post->post_title, |
| 168 |
'value' => $campaign_post->ID, |
| 169 |
); |
| 170 |
endforeach; |
| 171 |
// sory multidimensional array alphabetically by label. |
| 172 |
usort( |
| 173 |
$campaigns_for_dropdown, |
| 174 |
function ( $a, $b ) { |
| 175 |
return strcmp( $a['label'], $b['label'] ); |
| 176 |
} |
| 177 |
); |
| 178 |
endif; |
| 179 |
|
| 180 |
// add blank element. |
| 181 |
array_unshift( |
| 182 |
$campaigns_for_dropdown, |
| 183 |
array( |
| 184 |
'label' => '', |
| 185 |
'value' => '', |
| 186 |
) |
| 187 |
); |
| 188 |
|
| 189 |
$strings = array( |
| 190 |
'campaigns' => $campaigns_for_dropdown, |
| 191 |
'charitable_addons_page' => esc_url( admin_url( 'admin.php?page=charitable-addons' ) ), |
| 192 |
'version' => '1.8.0', |
| 193 |
'charitable_assets_dir' => apply_filters( |
| 194 |
'charitable_campaign_builder_charitable_assets_dir', |
| 195 |
charitable()->get_path( 'directory', false ) . 'assets/' |
| 196 |
), |
| 197 |
); |
| 198 |
|
| 199 |
return $strings; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Returns and/or create the single instance of this class. |
| 204 |
* |
| 205 |
* @since 1.8.0 |
| 206 |
* |
| 207 |
* @return Charitable_Admin |
| 208 |
*/ |
| 209 |
public static function get_instance() { |
| 210 |
if ( is_null( self::$instance ) ) { |
| 211 |
self::$instance = new self(); |
| 212 |
} |
| 213 |
|
| 214 |
return self::$instance; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
new Charitable_Admin_Blocks(); |
| 219 |
|
| 220 |
endif; |
| 221 |
|