| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if ( ! defined( 'WPINC' ) ) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Adds the 'wpLingua' category to the Gutenberg block categories |
| 11 |
* |
| 12 |
* @param array $block_categories The block categories. |
| 13 |
* @return array The updated block categories. |
| 14 |
*/ |
| 15 |
function wplng_block_category( $block_categories ) { |
| 16 |
|
| 17 |
// Create a new category for the wpLingua language switcher block |
| 18 |
$new_category = array( |
| 19 |
'slug' => 'wplingua', |
| 20 |
'title' => 'wpLingua', |
| 21 |
); |
| 22 |
|
| 23 |
// Find the index of the 'design' category in the block categories array |
| 24 |
$design_index = array_search( 'design', array_column( $block_categories, 'slug' ) ); |
| 25 |
|
| 26 |
if ( $design_index !== false ) { |
| 27 |
// If the 'design' category is found, insert the new category after it |
| 28 |
array_splice( $block_categories, $design_index + 1, 0, array( $new_category ) ); |
| 29 |
} else { |
| 30 |
// If the 'design' category is not found, append the new category to the end of the array |
| 31 |
$block_categories[] = $new_category; |
| 32 |
} |
| 33 |
|
| 34 |
return $block_categories; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Registers the block with the name 'wplingua/languages-switcher' |
| 40 |
* |
| 41 |
* @see https://developer.wordpress.org/block-editor/developers/block-api/block-registration/ |
| 42 |
*/ |
| 43 |
function wplng_register_block() { |
| 44 |
|
| 45 |
$keywords = array_unique( |
| 46 |
array( |
| 47 |
|
| 48 |
// Untranslated keywords |
| 49 |
'language', |
| 50 |
'switcher', |
| 51 |
'multilanguage', |
| 52 |
'multilingual', |
| 53 |
'translate', |
| 54 |
'translation', |
| 55 |
'flag', |
| 56 |
'flags', |
| 57 |
|
| 58 |
// Not editable |
| 59 |
'wpLingua', |
| 60 |
|
| 61 |
// Translated keywords |
| 62 |
__( 'language', 'wplingua' ), |
| 63 |
__( 'switcher', 'wplingua' ), |
| 64 |
__( 'multilanguage', 'wplingua' ), |
| 65 |
__( 'multilingual', 'wplingua' ), |
| 66 |
__( 'translate', 'wplingua' ), |
| 67 |
__( 'translation', 'wplingua' ), |
| 68 |
__( 'flag', 'wplingua' ), |
| 69 |
__( 'flags', 'wplingua' ), |
| 70 |
|
| 71 |
) |
| 72 |
); |
| 73 |
|
| 74 |
register_block_type( |
| 75 |
'wplingua/languages-switcher', |
| 76 |
array( |
| 77 |
'title' => __( 'wpLingua: languages switcher', 'wplingua' ), |
| 78 |
'description' => __( 'Add a language switcher to your page.', 'wplingua' ), |
| 79 |
'icon' => 'translation', |
| 80 |
'category' => 'wplingua', |
| 81 |
'render_callback' => 'wplng_render_switcher_block', |
| 82 |
'keywords' => $keywords, |
| 83 |
'attributes' => array( |
| 84 |
'style' => array( |
| 85 |
'type' => 'string', |
| 86 |
'default' => '', |
| 87 |
'enum' => array_merge( |
| 88 |
array( '' ), |
| 89 |
array_keys( wplng_data_switcher_valid_style() ) |
| 90 |
), |
| 91 |
), |
| 92 |
'title' => array( |
| 93 |
'type' => 'string', |
| 94 |
'default' => '', |
| 95 |
'enum' => array_merge( |
| 96 |
array( '' ), |
| 97 |
array_keys( wplng_data_switcher_valid_name_format() ) |
| 98 |
), |
| 99 |
), |
| 100 |
'flags' => array( |
| 101 |
'type' => 'string', |
| 102 |
'default' => '', |
| 103 |
'enum' => array_merge( |
| 104 |
array( '' ), |
| 105 |
array_keys( wplng_data_switcher_valid_flags_style() ) |
| 106 |
), |
| 107 |
), |
| 108 |
'theme' => array( |
| 109 |
'type' => 'string', |
| 110 |
'default' => '', |
| 111 |
'enum' => array_merge( |
| 112 |
array( '' ), |
| 113 |
array_keys( wplng_data_switcher_valid_theme() ) |
| 114 |
), |
| 115 |
), |
| 116 |
), |
| 117 |
'editor_script' => 'wplingua-block-switcher', |
| 118 |
) |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Renders the block content. |
| 125 |
* |
| 126 |
* @param array $attributes The block attributes. |
| 127 |
* @return string The block content. |
| 128 |
*/ |
| 129 |
function wplng_render_switcher_block( $attributes ) { |
| 130 |
|
| 131 |
if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
| 132 |
return wplng_get_switcher_html( |
| 133 |
array_merge( |
| 134 |
$attributes, |
| 135 |
array( 'class' => 'switcher-preview' ) |
| 136 |
) |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
return wplng_get_switcher_html( $attributes ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Registers the JavaScript and CSS files for the block editor. |
| 146 |
* |
| 147 |
* This function enqueues the necessary scripts and styles for the wpLingua |
| 148 |
* language switcher block in the Gutenberg editor. It also localizes script |
| 149 |
* data for use in JavaScript and includes necessary dependencies like jQuery. |
| 150 |
*/ |
| 151 |
function wplng_register_block_assets() { |
| 152 |
|
| 153 |
// Enqueue the script for rendering the language switcher block in the editor |
| 154 |
wp_enqueue_script( |
| 155 |
'wplingua-block-switcher', |
| 156 |
plugins_url() . '/wplingua/assets/js/block-switcher.js', |
| 157 |
array( |
| 158 |
'wp-blocks', |
| 159 |
'wp-editor', |
| 160 |
'wp-server-side-render', |
| 161 |
), |
| 162 |
WPLNG_PLUGIN_VERSION |
| 163 |
); |
| 164 |
|
| 165 |
// Localize script data for use in JavaScript |
| 166 |
wp_localize_script( |
| 167 |
'wplingua-block-switcher', |
| 168 |
'wplngI18nGutenberg', |
| 169 |
array( |
| 170 |
'label' => array( |
| 171 |
'title' => esc_html__( 'Languages switcher', 'wplingua' ), |
| 172 |
'description' => esc_html__( 'Display the wpLingua languages switcher.', 'wplingua' ), |
| 173 |
'default' => esc_html__( 'Default', 'wplingua' ), |
| 174 |
), |
| 175 |
'input' => array( |
| 176 |
'style' => esc_html__( 'Layout: ', 'wplingua' ), |
| 177 |
'title' => esc_html__( 'Displayed names: ', 'wplingua' ), |
| 178 |
'flags' => esc_html__( 'Flags style: ', 'wplingua' ), |
| 179 |
'theme' => esc_html__( 'Color theme: ', 'wplingua' ), |
| 180 |
), |
| 181 |
'style' => wplng_data_switcher_valid_style(), |
| 182 |
'title' => wplng_data_switcher_valid_name_format(), |
| 183 |
'flags' => wplng_data_switcher_valid_flags_style(), |
| 184 |
'theme' => wplng_data_switcher_valid_theme(), |
| 185 |
) |
| 186 |
); |
| 187 |
|
| 188 |
// Enqueue wpLingua main JavaScript file |
| 189 |
wp_enqueue_script( |
| 190 |
'wplingua-script', |
| 191 |
plugins_url() . '/wplingua/assets/js/front.js', |
| 192 |
array(), |
| 193 |
WPLNG_PLUGIN_VERSION |
| 194 |
); |
| 195 |
|
| 196 |
// Enqueue wpLingua frontend CSS style |
| 197 |
wp_enqueue_style( |
| 198 |
'wplingua', |
| 199 |
plugins_url() . '/wplingua/assets/css/front.css', |
| 200 |
array(), |
| 201 |
WPLNG_PLUGIN_VERSION |
| 202 |
); |
| 203 |
} |
| 204 |
|