class-do-css.php
5 years ago
class-enqueue-css.php
5 years ago
class-plugin-update.php
5 years ago
class-render-blocks.php
5 years ago
class-rest.php
5 years ago
class-settings.php
5 years ago
dashboard.php
5 years ago
defaults.php
5 years ago
functions.php
5 years ago
general.php
5 years ago
generate-css.php
5 years ago
general.php
247 lines
| 1 | <?php |
| 2 | /** |
| 3 | * General actions and filters. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | add_action( 'enqueue_block_editor_assets', 'generateblocks_do_block_editor_assets' ); |
| 13 | /** |
| 14 | * Enqueue Gutenberg block assets for backend editor. |
| 15 | * |
| 16 | * @uses {wp-blocks} for block type registration & related functions. |
| 17 | * @uses {wp-element} for WP Element abstraction — structure of blocks. |
| 18 | * @uses {wp-i18n} to internationalize the block's text. |
| 19 | * @uses {wp-editor} for WP editor styles. |
| 20 | * @since 0.1 |
| 21 | */ |
| 22 | function generateblocks_do_block_editor_assets() { |
| 23 | wp_enqueue_script( |
| 24 | 'generateblocks', |
| 25 | GENERATEBLOCKS_DIR_URL . 'dist/blocks.js', |
| 26 | array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'wp-compose', 'wp-data' ), |
| 27 | filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.js' ), |
| 28 | true |
| 29 | ); |
| 30 | |
| 31 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 32 | wp_set_script_translations( 'generateblocks', 'generateblocks' ); |
| 33 | } |
| 34 | |
| 35 | wp_enqueue_style( |
| 36 | 'generateblocks', |
| 37 | GENERATEBLOCKS_DIR_URL . 'dist/blocks.css', |
| 38 | array( 'wp-edit-blocks' ), |
| 39 | filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.css' ) |
| 40 | ); |
| 41 | |
| 42 | $image_sizes = get_intermediate_image_sizes(); |
| 43 | $image_sizes = array_diff( $image_sizes, array( '1536x1536', '2048x2048' ) ); |
| 44 | $image_sizes[] = 'full'; |
| 45 | |
| 46 | wp_localize_script( |
| 47 | 'generateblocks', |
| 48 | 'generateBlocksInfo', |
| 49 | array( |
| 50 | 'isGeneratePress' => defined( 'GENERATE_VERSION' ), |
| 51 | 'hasCustomFields' => post_type_supports( get_post_type(), 'custom-fields' ), |
| 52 | 'hasWideAlignSupport' => current_theme_supports( 'align-wide' ), |
| 53 | 'imageSizes' => $image_sizes, |
| 54 | 'svgShapes' => generateblocks_get_svg_shapes(), |
| 55 | 'syncResponsivePreviews' => generateblocks_get_option( 'sync_responsive_previews' ), |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | if ( function_exists( 'generate_get_color_defaults' ) ) { |
| 60 | $color_settings = wp_parse_args( |
| 61 | get_option( 'generate_settings', array() ), |
| 62 | generate_get_color_defaults() |
| 63 | ); |
| 64 | |
| 65 | $generatepressDefaultStyling = apply_filters( |
| 66 | 'generateblocks_gp_default_styling', |
| 67 | array( |
| 68 | 'buttonBackground' => $color_settings['form_button_background_color'], |
| 69 | 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'], |
| 70 | 'buttonText' => $color_settings['form_button_text_color'], |
| 71 | 'buttonTextHover' => $color_settings['form_button_text_color_hover'], |
| 72 | 'buttonPaddingTop' => '10px', |
| 73 | 'buttonPaddingRight' => '20px', |
| 74 | 'buttonPaddingBottom' => '10px', |
| 75 | 'buttonPaddingLeft' => '20px', |
| 76 | ) |
| 77 | ); |
| 78 | |
| 79 | $css = sprintf( |
| 80 | '.gb-button.button { |
| 81 | background-color: %1$s; |
| 82 | color: %2$s; |
| 83 | padding-top: %3$s; |
| 84 | padding-right: %4$s; |
| 85 | padding-bottom: %5$s; |
| 86 | padding-left: %6$s; |
| 87 | }', |
| 88 | $generatepressDefaultStyling['buttonBackground'], |
| 89 | $generatepressDefaultStyling['buttonText'], |
| 90 | $generatepressDefaultStyling['buttonPaddingTop'], |
| 91 | $generatepressDefaultStyling['buttonPaddingRight'], |
| 92 | $generatepressDefaultStyling['buttonPaddingBottom'], |
| 93 | $generatepressDefaultStyling['buttonPaddingLeft'] |
| 94 | ); |
| 95 | |
| 96 | $css .= sprintf( |
| 97 | '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus { |
| 98 | background-color: %1$s; |
| 99 | color: %2$s; |
| 100 | }', |
| 101 | $generatepressDefaultStyling['buttonBackgroundHover'], |
| 102 | $generatepressDefaultStyling['buttonTextHover'] |
| 103 | ); |
| 104 | |
| 105 | wp_add_inline_style( 'generateblocks', $css ); |
| 106 | } |
| 107 | |
| 108 | $defaults = generateblocks_get_block_defaults(); |
| 109 | |
| 110 | wp_localize_script( |
| 111 | 'generateblocks', |
| 112 | 'generateBlocksDefaults', |
| 113 | $defaults |
| 114 | ); |
| 115 | |
| 116 | wp_localize_script( |
| 117 | 'generateblocks', |
| 118 | 'generateBlocksStyling', |
| 119 | generateblocks_get_default_styles() |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | add_filter( 'block_categories', 'generateblocks_do_category' ); |
| 124 | /** |
| 125 | * Add GeneratePress category to Gutenberg. |
| 126 | * |
| 127 | * @param array $categories Existing categories. |
| 128 | * @since 0.1 |
| 129 | */ |
| 130 | function generateblocks_do_category( $categories ) { |
| 131 | return array_merge( |
| 132 | array( |
| 133 | array( |
| 134 | 'slug' => 'generateblocks', |
| 135 | 'title' => __( 'GenerateBlocks', 'generateblocks' ), |
| 136 | ), |
| 137 | ), |
| 138 | $categories |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' ); |
| 143 | add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' ); |
| 144 | /** |
| 145 | * Do Google Fonts. |
| 146 | * |
| 147 | * @since 0.1 |
| 148 | */ |
| 149 | function generateblocks_do_google_fonts() { |
| 150 | $fonts_url = generateblocks_get_google_fonts_uri(); |
| 151 | |
| 152 | if ( $fonts_url ) { |
| 153 | wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | add_action( 'init', 'generateblocks_register_meta' ); |
| 158 | /** |
| 159 | * Register our post meta. |
| 160 | * |
| 161 | * @since 0.1 |
| 162 | */ |
| 163 | function generateblocks_register_meta() { |
| 164 | register_meta( |
| 165 | 'post', |
| 166 | '_generate-full-width-content', |
| 167 | array( |
| 168 | 'show_in_rest' => true, |
| 169 | 'auth_callback' => '__return_true', |
| 170 | 'single' => true, |
| 171 | ) |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | add_filter( 'generateblocks_css_print_method', 'generateblocks_set_css_print_method' ); |
| 176 | /** |
| 177 | * Set our CSS print method. |
| 178 | * |
| 179 | * @param string $method Existing method. |
| 180 | */ |
| 181 | function generateblocks_set_css_print_method( $method ) { |
| 182 | return generateblocks_get_option( 'css_print_method' ); |
| 183 | } |
| 184 | |
| 185 | add_filter( 'excerpt_allowed_blocks', 'generateblocks_set_excerpt_allowed_blocks' ); |
| 186 | /** |
| 187 | * Add blocks that can be displayed in post excerpts. |
| 188 | * |
| 189 | * @param array $allowed Existing allowed blocks. |
| 190 | * @since 1.0 |
| 191 | */ |
| 192 | function generateblocks_set_excerpt_allowed_blocks( $allowed ) { |
| 193 | $allowed[] = 'generateblocks/headline'; |
| 194 | $allowed[] = 'generateblocks/container'; |
| 195 | |
| 196 | return $allowed; |
| 197 | } |
| 198 | |
| 199 | add_filter( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 ); |
| 200 | /** |
| 201 | * Add shape divider to Container. |
| 202 | * |
| 203 | * @since 1.2.0 |
| 204 | * @param string $output The current block output. |
| 205 | * @param array $attributes The current block attributes. |
| 206 | */ |
| 207 | function generateblocks_do_shape_divider( $output, $attributes ) { |
| 208 | $defaults = generateblocks_get_block_defaults(); |
| 209 | |
| 210 | $settings = wp_parse_args( |
| 211 | $attributes, |
| 212 | $defaults['container'] |
| 213 | ); |
| 214 | |
| 215 | if ( ! empty( $settings['shapeDividers'] ) ) { |
| 216 | $shapes = generateblocks_get_svg_shapes(); |
| 217 | $shape_values = array(); |
| 218 | |
| 219 | foreach ( $shapes as $group => $data ) { |
| 220 | if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) { |
| 221 | foreach ( $data['svgs'] as $key => $shape ) { |
| 222 | $shape_values[ $key ] = $shape['icon']; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | $output .= '<div class="gb-shapes">'; |
| 228 | |
| 229 | foreach ( (array) $settings['shapeDividers'] as $index => $option ) { |
| 230 | if ( ! empty( $option['shape'] ) ) { |
| 231 | if ( isset( $shape_values[ $option['shape'] ] ) ) { |
| 232 | $shapeNumber = $index + 1; |
| 233 | |
| 234 | $output .= sprintf( |
| 235 | '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>', |
| 236 | $shape_values[ $option['shape'] ] |
| 237 | ); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | $output .= '</div>'; |
| 243 | } |
| 244 | |
| 245 | return $output; |
| 246 | } |
| 247 |