| 1 |
<?php |
| 2 |
/** |
| 3 |
* The editor integration of the plugin |
| 4 |
* |
| 5 |
* @package civist |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Configures Civist integration with classic and block editor |
| 10 |
*/ |
| 11 |
class Civist_Editor { |
| 12 |
/** |
| 13 |
* The plugin name. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $plugin_name; |
| 18 |
/** |
| 19 |
* The plugin file. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $plugin_file; |
| 24 |
/** |
| 25 |
* The plugin slug. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $plugin_slug; |
| 30 |
/** |
| 31 |
* The plugin scripts. |
| 32 |
* |
| 33 |
* @var Civist_Script |
| 34 |
*/ |
| 35 |
private $scripts; |
| 36 |
/** |
| 37 |
* The editor button id. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
private $editor_button_id; |
| 42 |
/** |
| 43 |
* The editor button click handler name. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $editor_button_click_handler_name; |
| 48 |
|
| 49 |
const EMBED_BLOCK_NAME = 'civist/embed'; |
| 50 |
const FORM_BLOCK_NAME = 'civist/form'; |
| 51 |
const PROGRESS_BLOCK_NAME = 'civist/progress'; |
| 52 |
const CIVIST_BLOCK_NAME = 'civist/civist'; |
| 53 |
|
| 54 |
/** |
| 55 |
* The Civist_Admin class constructor |
| 56 |
* |
| 57 |
* @param string $plugin_name The name of the plugin. |
| 58 |
* @param string $plugin_file The file of the plugin. |
| 59 |
* @param string $plugin_slug The slug of the plugin. |
| 60 |
* @param Civist_Scripts $scripts The plugin scripts. |
| 61 |
*/ |
| 62 |
public function __construct( $plugin_name, $plugin_file, $plugin_slug, Civist_Scripts $scripts ) { |
| 63 |
$this->plugin_name = $plugin_name; |
| 64 |
$this->plugin_file = $plugin_file; |
| 65 |
$this->plugin_slug = $plugin_slug; |
| 66 |
$this->scripts = $scripts; |
| 67 |
$this->editor_button_id = $plugin_slug . '_editor_button'; |
| 68 |
$this->editor_button_click_handler_name = $plugin_slug . '_handle_media_button_click'; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Renders the plugin media button for embedding content into posts/pages. |
| 73 |
*/ |
| 74 |
public function add_petition_media_button() { |
| 75 |
/* translators: The button to open the modal to add a petition to a post/page. */ |
| 76 |
$text = _x( 'Civist', 'wp.plugin.editor.button', 'civist' ); |
| 77 |
?> |
| 78 |
<button type="button" class="button" style="padding-left: 5px;" |
| 79 |
id="<?php echo( esc_html( $this->editor_button_id ) ); ?>" |
| 80 |
onclick="<?php echo( esc_js( $this->editor_button_click_handler_name ) . '()' ); ?>" |
| 81 |
> |
| 82 |
<span class="wp-media-buttons-icon dashicons-civist_symbol_wp" style="font-size: 18px; vertical-align: sub;"></span><?php echo( esc_html( $text ) ); ?> |
| 83 |
</button> |
| 84 |
<?php |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Register and enqueue the plugin scripts for the classic editor |
| 89 |
*/ |
| 90 |
public function enqueue_editor_scripts() { |
| 91 |
include 'civist-scripts.php'; // exposes $webpack-files variable. |
| 92 |
$this->scripts->enqueue_editor_scripts( $civist_webpack_files, $this->editor_button_click_handler_name, $this::EMBED_BLOCK_NAME, $this::FORM_BLOCK_NAME, $this::PROGRESS_BLOCK_NAME, $this::CIVIST_BLOCK_NAME ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Register the plugin blocks for the block editor |
| 97 |
*/ |
| 98 |
public function register_plugin_blocks() { |
| 99 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
$this->register_plugin_embed_block(); |
| 103 |
$this->register_plugin_form_block(); |
| 104 |
$this->register_plugin_progress_block(); |
| 105 |
$this->register_plugin_civist_block(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Register the (deprecated) plugin embed block for the block editor |
| 110 |
*/ |
| 111 |
private function register_plugin_embed_block() { |
| 112 |
/* translators: The title of the deprecated Civist block. */ |
| 113 |
$title = _x( 'Civist (deprecated)', 'wp.plugin.block.civist_embed.title', 'civist' ); |
| 114 |
/* translators: The description of the deprecated Civist block. */ |
| 115 |
$description = _x( 'This block is deprecated. Please change the block type to the new Civist block.', 'wp.plugin.block.civist_embed.description', 'civist' ); |
| 116 |
register_block_type( |
| 117 |
$this::EMBED_BLOCK_NAME, |
| 118 |
array( |
| 119 |
'title' => $title, |
| 120 |
'description' => $description, |
| 121 |
'category' => 'embed', |
| 122 |
'keywords' => array( 'petition', 'donation form', 'embed' ), |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
/** |
| 127 |
* Register the plugin form block for the block editor |
| 128 |
*/ |
| 129 |
private function register_plugin_form_block() { |
| 130 |
/* translators: The title of the Civist block that contains a petition or donation form using the block editor. */ |
| 131 |
$title = _x( 'Civist form', 'wp.plugin.block.civist_form.title', 'civist' ); |
| 132 |
/* translators: The description of the Civist block that contins a petition or donation form using the block editor. */ |
| 133 |
$description = _x( 'The form', 'wp.plugin.block.civist_form.description', 'civist' ); |
| 134 |
|
| 135 |
register_block_type( |
| 136 |
$this::FORM_BLOCK_NAME, |
| 137 |
array( |
| 138 |
'title' => $title, |
| 139 |
'description' => $description, |
| 140 |
'category' => 'embed', |
| 141 |
'keywords' => array( 'petition', 'donation form', 'embed' ), |
| 142 |
) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Register the plugin progress block for the block editor |
| 149 |
*/ |
| 150 |
private function register_plugin_progress_block() { |
| 151 |
include 'civist-blocks-scripts.php'; // exposes $civist_webpack_files variable. |
| 152 |
|
| 153 |
/* translators: The title of the Civist block that contains a petition's progress using the block editor. */ |
| 154 |
$title = _x( 'Civist progress', 'wp.plugin.block.civist_progress.title', 'civist' ); |
| 155 |
/* translators: The description of the Civist block for that contins a petition's progress using the block editor. */ |
| 156 |
$description = _x( 'The progress', 'wp.plugin.block.civist_progress.description', 'civist' ); |
| 157 |
|
| 158 |
register_block_type( |
| 159 |
$this::PROGRESS_BLOCK_NAME, |
| 160 |
array( |
| 161 |
'title' => $title, |
| 162 |
'description' => $description, |
| 163 |
'category' => 'embed', |
| 164 |
'keywords' => array( 'petition', 'progress', 'embed' ), |
| 165 |
) |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Register the plugin main block for the block editor |
| 171 |
*/ |
| 172 |
private function register_plugin_civist_block() { |
| 173 |
include 'civist-blocks-scripts.php'; // exposes $civist_webpack_files variable. |
| 174 |
$script = 'civist_blocks_embed'; |
| 175 |
$editor_script = 'civist_blocks'; |
| 176 |
$style = 'civist_blocks_style'; |
| 177 |
wp_register_script( $editor_script, plugin_dir_url( __FILE__ ) . $civist_webpack_files->editor->entry, array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-url' ), $civist_webpack_files->editor->hash, true ); |
| 178 |
wp_localize_script( |
| 179 |
$editor_script, |
| 180 |
'civist', |
| 181 |
array_merge( |
| 182 |
$this->scripts->get_management_configuration(), |
| 183 |
array( |
| 184 |
'openEditorModalCallback' => $this->editor_button_click_handler_name, |
| 185 |
'embedBlockName' => $this::EMBED_BLOCK_NAME, |
| 186 |
'formBlockName' => $this::FORM_BLOCK_NAME, |
| 187 |
'progressBlockName' => $this::PROGRESS_BLOCK_NAME, |
| 188 |
'civistBlockName' => $this::CIVIST_BLOCK_NAME, |
| 189 |
) |
| 190 |
) |
| 191 |
); |
| 192 |
wp_register_script( $script, plugin_dir_url( __FILE__ ) . $civist_webpack_files->embed->entry, array( 'wp-dom-ready' ), $civist_webpack_files->embed->hash, true ); |
| 193 |
wp_localize_script( $script, 'civist_public', $this->scripts->get_public_configuration() ); |
| 194 |
wp_register_style( $style, plugin_dir_url( __FILE__ ) . $civist_webpack_files->embed->css[0], array(), $civist_webpack_files->embed->hash ); |
| 195 |
|
| 196 |
/* translators: The title of the Civist block for embedding a petition or donation form using the block editor. */ |
| 197 |
$title = _x( 'Civist', 'wp.plugin.block.civist_civist.title', 'civist' ); |
| 198 |
/* translators: The description of the Civist block for embedding a petition or donation form using the block editor. */ |
| 199 |
$description = _x( 'Embed a Petition or a Donation Form', 'wp.plugin.block.civist_civist.description', 'civist' ); |
| 200 |
|
| 201 |
register_block_type( |
| 202 |
$this::CIVIST_BLOCK_NAME, |
| 203 |
array( |
| 204 |
'style' => $style, |
| 205 |
'editor_script' => $editor_script, |
| 206 |
'script' => $script, |
| 207 |
'title' => $title, |
| 208 |
'description' => $description, |
| 209 |
'category' => 'embed', |
| 210 |
'keywords' => array( 'petition', 'donation form', 'embed' ), |
| 211 |
) |
| 212 |
); |
| 213 |
} |
| 214 |
} |
| 215 |
|