CustomizerTrait.php
5 years ago
DefaultTemplateCustomizer.php
3 years ago
EmailSettingsPage.php
1 year ago
WPListTable.php
4 years ago
email-template-preview.php
5 years ago
DefaultTemplateCustomizer.php
337 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\EmailSettings; |
| 4 | |
| 5 | class DefaultTemplateCustomizer |
| 6 | { |
| 7 | use CustomizerTrait; |
| 8 | |
| 9 | const DBPREFIX = 'ppress_email'; |
| 10 | |
| 11 | const body_section = 'ppress_email_template_body'; |
| 12 | const header_section = 'ppress_email_template_header'; |
| 13 | const footer_section = 'ppress_email_template_footer'; |
| 14 | |
| 15 | protected static $site_title; |
| 16 | |
| 17 | public function __construct() |
| 18 | { |
| 19 | if ( ! empty($_REQUEST['ppress-preview-template'])) { |
| 20 | |
| 21 | $this->clean_up_customizer(); |
| 22 | $this->modify_customizer_publish_button(); |
| 23 | |
| 24 | add_action('customize_controls_enqueue_scripts', [$this, 'monkey_patch_customizer_payload']); |
| 25 | |
| 26 | self::$site_title = ppress_site_title(); |
| 27 | |
| 28 | add_action('customize_register', [$this, 'customizer_register'], -1); |
| 29 | |
| 30 | add_action('parse_request', [$this, 'include_customizer_template'], 1); |
| 31 | |
| 32 | add_action('customize_controls_init', [$this, 'set_customizer_urls']); |
| 33 | |
| 34 | add_action('customize_section_active', array($this, 'remove_sections'), 999999999, 2); |
| 35 | |
| 36 | // Remove all customizer panels. |
| 37 | add_action('customize_panel_active', '__return_false'); |
| 38 | |
| 39 | add_filter('gettext', array($this, 'rewrite_customizer_panel_description'), 10, 3); |
| 40 | |
| 41 | // rewrite panel name from blog name to PP email template. |
| 42 | add_filter('pre_option_blogname', function () { |
| 43 | return esc_html__('ProfilePress Email Template', 'wp-user-avatar'); |
| 44 | }); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public static function get_customizer_value($setting) |
| 49 | { |
| 50 | $settings = get_option(self::DBPREFIX, []); |
| 51 | |
| 52 | $default = self::defaults($setting); |
| 53 | |
| 54 | if ($setting == 'header_text' && empty($default)) $default = ppress_site_title(); |
| 55 | |
| 56 | $default = str_replace('{{siteTitle}}', ppress_site_title(), $default); |
| 57 | |
| 58 | return ! empty($settings[$setting]) ? $settings[$setting] : $default; |
| 59 | } |
| 60 | |
| 61 | public function monkey_patch_customizer_payload() |
| 62 | { |
| 63 | wp_add_inline_script('customize-controls', '(function ( api ) { |
| 64 | api.bind( "ready", function () { |
| 65 | var _query = api.previewer.query; |
| 66 | api.previewer.query = function () { |
| 67 | var query = _query.call( this ); |
| 68 | query["ppress-preview-template"] = "true"; |
| 69 | return query; |
| 70 | }; |
| 71 | // needed to ensure save button is publising changes and not saving draft. |
| 72 | // esp for wp.com business hosting with save button set to draft by default. |
| 73 | api.state("selectedChangesetStatus").set("publish"); |
| 74 | }); |
| 75 | })( wp.customize );' |
| 76 | ); |
| 77 | |
| 78 | wp_add_inline_style( |
| 79 | 'customize-controls', |
| 80 | 'button#publish-settings {display: none !important;}#customize-save-button-wrapper .save.has-next-sibling {border-radius: 3px;}'); |
| 81 | } |
| 82 | |
| 83 | public function include_customizer_template() |
| 84 | { |
| 85 | if (is_customize_preview() && ! empty($_GET['ppress-preview-template']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ppress-preview-template')) { |
| 86 | include(dirname(__FILE__) . '/email-template-preview.php'); |
| 87 | exit; |
| 88 | } |
| 89 | |
| 90 | wp_safe_redirect(PPRESS_SETTINGS_EMAIL_SETTING_PAGE); |
| 91 | exit; |
| 92 | } |
| 93 | |
| 94 | public function set_customizer_urls() |
| 95 | { |
| 96 | global $wp_customize; |
| 97 | |
| 98 | $preview_url = add_query_arg( |
| 99 | '_wpnonce', |
| 100 | wp_create_nonce('ppress-preview-template'), |
| 101 | sprintf(home_url('/?ppress-preview-template=true')) |
| 102 | ); |
| 103 | |
| 104 | $return_url = PPRESS_SETTINGS_EMAIL_SETTING_PAGE; |
| 105 | |
| 106 | $wp_customize->set_preview_url($preview_url); |
| 107 | $wp_customize->set_return_url($return_url); |
| 108 | } |
| 109 | |
| 110 | public static function get_prefixed_id($setting) |
| 111 | { |
| 112 | return self::DBPREFIX . "[" . $setting . "]"; |
| 113 | } |
| 114 | |
| 115 | public function remove_sections($active, $section) |
| 116 | { |
| 117 | $sections_ids = [self::header_section, self::footer_section, self::body_section]; |
| 118 | |
| 119 | return in_array($section->id, $sections_ids); |
| 120 | } |
| 121 | |
| 122 | public function rewrite_customizer_panel_description($translations, $text, $domain) |
| 123 | { |
| 124 | if (strpos($text, 'Customizer allows you to preview changes to your site')) { |
| 125 | $translations = __( |
| 126 | 'The customizer allows you to design and preview ProfilePress default email template.', |
| 127 | 'wp-user-avatar' |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | return $translations; |
| 132 | } |
| 133 | |
| 134 | public function customizer_register($wp_customize) |
| 135 | { |
| 136 | remove_all_actions('customize_register'); // improve compatibility with hestia, generatepress themes etc |
| 137 | $this->sections($wp_customize); |
| 138 | $this->settings($wp_customize); |
| 139 | $this->controls($wp_customize); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param \WP_Customize_Manager $wp_customize |
| 144 | */ |
| 145 | public function sections($wp_customize) |
| 146 | { |
| 147 | $wp_customize->add_section(self::body_section, array( |
| 148 | 'title' => esc_html__('Body', 'wp-user-avatar'), |
| 149 | 'priority' => 10, |
| 150 | ) |
| 151 | ); |
| 152 | |
| 153 | $wp_customize->add_section(self::header_section, array( |
| 154 | 'title' => esc_html__('Header', 'wp-user-avatar'), |
| 155 | 'priority' => 10, |
| 156 | ) |
| 157 | ); |
| 158 | |
| 159 | $wp_customize->add_section(self::footer_section, array( |
| 160 | 'title' => esc_html__('Footer', 'wp-user-avatar'), |
| 161 | 'priority' => 20, |
| 162 | ) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | public static function defaults($setting) |
| 167 | { |
| 168 | $defaults = [ |
| 169 | 'background_color' => '#f2f4f6', |
| 170 | 'background_text_color' => '#a8aaaf', |
| 171 | 'content_background_color' => '#ffffff', |
| 172 | 'content_header_color' => '#333333', |
| 173 | 'content_text_color' => '#51545e', |
| 174 | 'header_text' => self::$site_title, |
| 175 | 'footer_text' => sprintf(esc_html__('© %s %s. All rights reserved.'), date('Y'), '{{siteTitle}}') |
| 176 | ]; |
| 177 | |
| 178 | return isset($defaults[$setting]) ? $defaults[$setting] : ''; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @param \WP_Customize_Manager $wp_customize |
| 183 | */ |
| 184 | public function settings($wp_customize) |
| 185 | { |
| 186 | $wp_customize->add_setting(self::get_prefixed_id('background_color'), [ |
| 187 | 'type' => 'option', |
| 188 | 'default' => self::defaults('background_color') |
| 189 | ]); |
| 190 | |
| 191 | $wp_customize->add_setting(self::get_prefixed_id('background_text_color'), [ |
| 192 | 'type' => 'option', |
| 193 | 'default' => self::defaults('background_text_color') |
| 194 | ]); |
| 195 | |
| 196 | $wp_customize->add_setting(self::get_prefixed_id('content_background_color'), [ |
| 197 | 'type' => 'option', |
| 198 | 'default' => self::defaults('content_background_color') |
| 199 | ]); |
| 200 | |
| 201 | $wp_customize->add_setting(self::get_prefixed_id('content_header_color'), [ |
| 202 | 'type' => 'option', |
| 203 | 'default' => self::defaults('content_header_color') |
| 204 | ]); |
| 205 | |
| 206 | $wp_customize->add_setting(self::get_prefixed_id('content_text_color'), [ |
| 207 | 'type' => 'option', |
| 208 | 'default' => self::defaults('content_text_color') |
| 209 | ]); |
| 210 | |
| 211 | $wp_customize->add_setting(self::get_prefixed_id('header_logo'), [ |
| 212 | 'type' => 'option' |
| 213 | ]); |
| 214 | |
| 215 | $wp_customize->add_setting(self::get_prefixed_id('header_text'), [ |
| 216 | 'default' => self::defaults('header_text'), |
| 217 | 'type' => 'option' |
| 218 | ]); |
| 219 | |
| 220 | $wp_customize->add_setting(self::get_prefixed_id('footer_text'), [ |
| 221 | 'default' => sprintf(esc_html__('© %s %s. All rights reserved.'), date('Y'), self::$site_title), |
| 222 | 'type' => 'option' |
| 223 | ]); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param \WP_Customize_Manager $wp_customize |
| 228 | */ |
| 229 | public function controls($wp_customize) |
| 230 | { |
| 231 | $wp_customize->add_control(new \WP_Customize_Color_Control( |
| 232 | $wp_customize, |
| 233 | self::get_prefixed_id('background_color'), |
| 234 | array( |
| 235 | 'label' => __('Background Color', 'wp-user-avatar'), |
| 236 | 'section' => self::body_section, |
| 237 | 'settings' => self::get_prefixed_id('background_color'), |
| 238 | 'priority' => 10 |
| 239 | ) |
| 240 | )); |
| 241 | |
| 242 | $wp_customize->add_control(new \WP_Customize_Color_Control( |
| 243 | $wp_customize, |
| 244 | self::get_prefixed_id('background_text_color'), |
| 245 | array( |
| 246 | 'label' => __('Background Text Color', 'wp-user-avatar'), |
| 247 | 'section' => self::body_section, |
| 248 | 'settings' => self::get_prefixed_id('background_text_color'), |
| 249 | 'priority' => 20 |
| 250 | ) |
| 251 | )); |
| 252 | $wp_customize->add_control(new \WP_Customize_Color_Control( |
| 253 | $wp_customize, |
| 254 | self::get_prefixed_id('content_background_color'), |
| 255 | array( |
| 256 | 'label' => __('Content Background Color', 'wp-user-avatar'), |
| 257 | 'section' => self::body_section, |
| 258 | 'settings' => self::get_prefixed_id('content_background_color'), |
| 259 | 'priority' => 30 |
| 260 | ) |
| 261 | )); |
| 262 | |
| 263 | $wp_customize->add_control(new \WP_Customize_Color_Control( |
| 264 | $wp_customize, |
| 265 | self::get_prefixed_id('content_header_color'), |
| 266 | array( |
| 267 | 'label' => __('Content Header Color', 'wp-user-avatar'), |
| 268 | 'section' => self::body_section, |
| 269 | 'settings' => self::get_prefixed_id('content_header_color'), |
| 270 | 'priority' => 40 |
| 271 | ) |
| 272 | )); |
| 273 | |
| 274 | $wp_customize->add_control(new \WP_Customize_Color_Control( |
| 275 | $wp_customize, |
| 276 | self::get_prefixed_id('content_text_color'), |
| 277 | array( |
| 278 | 'label' => __('Content Text Color', 'wp-user-avatar'), |
| 279 | 'section' => self::body_section, |
| 280 | 'settings' => self::get_prefixed_id('content_text_color'), |
| 281 | 'priority' => 40 |
| 282 | ) |
| 283 | )); |
| 284 | |
| 285 | $wp_customize->add_control(new \WP_Customize_Cropped_Image_Control( |
| 286 | $wp_customize, |
| 287 | self::get_prefixed_id('header_logo'), |
| 288 | array( |
| 289 | 'label' => __('Logo', 'wp-user-avatar'), |
| 290 | 'section' => self::header_section, |
| 291 | 'settings' => self::get_prefixed_id('header_logo'), |
| 292 | 'flex_width' => true, |
| 293 | 'flex_height' => true, |
| 294 | 'width' => 308, |
| 295 | 'height' => 48, |
| 296 | 'button_labels' => array( |
| 297 | 'select' => __('Select Logo', 'wp-user-avatar'), |
| 298 | 'change' => __('Change Logo', 'wp-user-avatar'), |
| 299 | 'default' => __('Default', 'wp-user-avatar'), |
| 300 | 'remove' => __('Remove', 'wp-user-avatar'), |
| 301 | 'placeholder' => __('No logo selected', 'wp-user-avatar'), |
| 302 | 'frame_title' => __('Select Logo', 'wp-user-avatar'), |
| 303 | 'frame_button' => __('Choose Logo', 'wp-user-avatar'), |
| 304 | ), |
| 305 | 'priority' => 50 |
| 306 | ) |
| 307 | )); |
| 308 | |
| 309 | $wp_customize->add_control(self::get_prefixed_id('header_text'), array( |
| 310 | 'label' => __('Header Text', 'wp-user-avatar'), |
| 311 | 'description' => __('This is used when template logo is not set.', 'wp-user-avatar'), |
| 312 | 'section' => self::header_section, |
| 313 | 'type' => 'text', |
| 314 | 'settings' => self::get_prefixed_id('header_text'), |
| 315 | 'priority' => 60 |
| 316 | )); |
| 317 | |
| 318 | $wp_customize->add_control(self::get_prefixed_id('footer_text'), array( |
| 319 | 'label' => __('Footer Text', 'wp-user-avatar'), |
| 320 | 'section' => self::footer_section, |
| 321 | 'type' => 'textarea', |
| 322 | 'settings' => self::get_prefixed_id('footer_text'), |
| 323 | 'priority' => 70 |
| 324 | )); |
| 325 | } |
| 326 | |
| 327 | public static function get_instance() |
| 328 | { |
| 329 | static $instance = null; |
| 330 | |
| 331 | if (is_null($instance)) { |
| 332 | $instance = new self(); |
| 333 | } |
| 334 | |
| 335 | return $instance; |
| 336 | } |
| 337 | } |