Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 6 | class Jetpack_Gutenberg { |
||
| 7 | /** |
||
| 8 | * Check if Gutenberg editor is available |
||
| 9 | * |
||
| 10 | * @since 6.7.0 |
||
| 11 | * |
||
| 12 | * @return bool |
||
| 13 | */ |
||
| 14 | public static function is_gutenberg_available() { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Load Gutenberg assets |
||
| 20 | * |
||
| 21 | * @since 6.7.0 |
||
| 22 | * |
||
| 23 | * @return void |
||
| 24 | */ |
||
| 25 | public static function enqueue_block_assets() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Load Gutenberg editor assets |
||
| 80 | * |
||
| 81 | * @since 6.7.0 |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public static function enqueue_block_editor_assets() { |
||
| 86 | if ( ! self::should_load_blocks() ) { |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | $rtl = is_rtl() ? '.rtl' : ''; |
||
| 91 | |||
| 92 | /** This filter is already documented above */ |
||
| 93 | View Code Duplication | if ( apply_filters( 'jetpack_gutenberg_cdn', false ) ) { |
|
| 94 | $cdn_base = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks'; |
||
| 95 | $editor_script = "$cdn_base/editor.js"; |
||
| 96 | $editor_style = "$cdn_base/editor$rtl.css"; |
||
| 97 | |||
| 98 | /** This filter is already documented above */ |
||
| 99 | $version = apply_filters( 'jetpack_gutenberg_cdn_cache_buster', sprintf( '%s-%s', gmdate( 'd-m-Y' ), JETPACK__VERSION ) ); |
||
| 100 | } else { |
||
| 101 | $editor_script = plugins_url( '_inc/blocks/editor.js', JETPACK__PLUGIN_FILE ); |
||
| 102 | $editor_style = plugins_url( "_inc/blocks/editor$rtl.css", JETPACK__PLUGIN_FILE ); |
||
| 103 | $version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' ) |
||
| 104 | ? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' ) |
||
| 105 | : JETPACK__VERSION; |
||
| 106 | } |
||
| 107 | |||
| 108 | wp_enqueue_script( |
||
| 109 | 'jetpack-blocks-editor', |
||
| 110 | $editor_script, |
||
| 111 | array( |
||
| 112 | 'lodash', |
||
| 113 | 'wp-api-fetch', |
||
| 114 | 'wp-blocks', |
||
| 115 | 'wp-components', |
||
| 116 | 'wp-compose', |
||
| 117 | 'wp-data', |
||
| 118 | 'wp-date', |
||
| 119 | 'wp-edit-post', |
||
| 120 | 'wp-editor', |
||
| 121 | 'wp-element', |
||
| 122 | 'wp-hooks', |
||
| 123 | 'wp-i18n', |
||
| 124 | 'wp-keycodes', |
||
| 125 | 'wp-plugins', |
||
| 126 | 'wp-token-list', |
||
| 127 | 'wp-url', |
||
| 128 | ), |
||
| 129 | $version |
||
| 130 | ); |
||
| 131 | |||
| 132 | wp_localize_script( |
||
| 133 | 'jetpack-blocks-editor', |
||
| 134 | 'Jetpack_Block_Assets_Base_Url', |
||
| 135 | plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE ) |
||
| 136 | ); |
||
| 137 | |||
| 138 | Jetpack::setup_wp_i18n_locale_data(); |
||
| 139 | |||
| 140 | wp_enqueue_style( 'jetpack-blocks-editor', $editor_style, array(), $version ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Check whether conditions indicate Gutenberg blocks should be loaded |
||
| 145 | * |
||
| 146 | * Loading blocks is enabled by default and may be disabled via filter: |
||
| 147 | * add_filter( 'jetpack_gutenberg', '__return_false' ); |
||
| 148 | * |
||
| 149 | * @since 6.7.0 |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public static function should_load_blocks() { |
||
| 167 | } |
||
| 168 |