class-do-css.php
6 years ago
class-enqueue-css.php
6 years ago
class-settings.php
6 years ago
dashboard.php
6 years ago
defaults.php
6 years ago
functions.php
6 years ago
general.php
6 years ago
generate-css.php
6 years ago
general.php
198 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.build.js', |
| 26 | array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), |
| 27 | filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.build.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_script( |
| 36 | 'generateblocks-dompurify', |
| 37 | GENERATEBLOCKS_DIR_URL . 'assets/js/purify.min.js', |
| 38 | array( 'generateblocks' ), |
| 39 | filemtime( GENERATEBLOCKS_DIR . 'assets/js/purify.min.js' ), |
| 40 | true |
| 41 | ); |
| 42 | |
| 43 | wp_enqueue_style( |
| 44 | 'generateblocks', |
| 45 | GENERATEBLOCKS_DIR_URL . 'dist/blocks.editor.build.css', |
| 46 | array( 'wp-edit-blocks' ), |
| 47 | filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.editor.build.css' ) |
| 48 | ); |
| 49 | |
| 50 | wp_localize_script( |
| 51 | 'generateblocks', |
| 52 | 'generateBlocksInfo', |
| 53 | array( |
| 54 | 'isGeneratePress' => defined( 'GENERATE_VERSION' ), |
| 55 | 'hasCustomFields' => post_type_supports( get_post_type(), 'custom-fields' ), |
| 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 |