|
| 1 | +<?php |
| 2 | +namespace VendorName\PluginName\Settings; |
| 3 | +use VendorName\PluginName\Plugin; |
| 4 | +use Inc2734\WP_Customizer_Framework\Customizer_Framework; |
| 5 | +use WordPress_ToolKit\Helpers\StringHelper; |
| 6 | + |
| 7 | +/** |
| 8 | + * Add custom section to Customizer |
| 9 | + * |
| 10 | + * @link https://github.com/inc2734/wp-customizer-framework |
| 11 | + * @since 0.4.0 |
| 12 | + */ |
| 13 | +class Customizer_Options extends Plugin { |
| 14 | + |
| 15 | + protected $customizer; |
| 16 | + |
| 17 | + function __construct() { |
| 18 | + |
| 19 | + // Create a default section name based on the plugin's slug |
| 20 | + $slug = StringHelper::underscores( self::$config->get( 'plugin/slug' ) ); |
| 21 | + |
| 22 | + // Define Customizer sections and fields |
| 23 | + $this->customizer = Customizer_Framework::init(); |
| 24 | + |
| 25 | + // Add new panel |
| 26 | + $panel = $this->customizer->panel( $this->prefix( $slug ) . '_panel', [ |
| 27 | + 'title' => self::$config->get( 'plugin/meta/Name' ) |
| 28 | + ]); |
| 29 | + |
| 30 | + // Add new section |
| 31 | + $section = $this->customizer->section( $this->prefix( 'my-plugin-section' ), [ |
| 32 | + 'title' => __( 'My Plugin Section', self::$textdomain ), |
| 33 | + 'description' => __( 'Example Customizer Section', self::$textdomain ) |
| 34 | + ]); |
| 35 | + |
| 36 | + // Define fields |
| 37 | + $controls = [ |
| 38 | + $this->customizer->control( 'file', $this->prefix( 'favicon' ), [ |
| 39 | + 'label' => __( 'Favicon', self::$textdomain ), |
| 40 | + //'default' => get_home_url( null, 'favicon.ico' ) |
| 41 | + ]), |
| 42 | + $this->customizer->control( 'color', $this->prefix( 'hyperlink-color' ), [ |
| 43 | + 'label' => __( 'Hyperlink Color', self::$textdomain ), |
| 44 | + 'default' => '#007acc' |
| 45 | + ]), |
| 46 | + $this->customizer->control( 'checkbox', $this->prefix( 'hyperlink-underline' ), [ |
| 47 | + 'label' => __( 'Underline Hyperlinks', self::$textdomain ), |
| 48 | + 'default' => false, |
| 49 | + 'description' => __( 'Add underline to hyperlinks.', self::$textdomain ) |
| 50 | + ]), |
| 51 | + $this->customizer->control( 'color', $this->prefix( 'h2-color' ), [ |
| 52 | + 'label' => __( 'H2 Header Color', self::$textdomain ), |
| 53 | + 'default' => '#1A1A1A' |
| 54 | + ]) |
| 55 | + ]; |
| 56 | + |
| 57 | + // Add panel with section and fields |
| 58 | + foreach( $controls as $control ) { |
| 59 | + $control->join( $section )->join( $panel ); |
| 60 | + } |
| 61 | + |
| 62 | + // Initialize Customizer options |
| 63 | + add_action( 'wp_loaded', array( $this, 'init_customizer_options' ) ); |
| 64 | + |
| 65 | + // Inject favicon link into page head |
| 66 | + add_action( 'wp_head', array( $this, 'inject_customizer_options'), 1 ); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Initialize Customizer option sections and fields |
| 72 | + * @since 0.4.0 |
| 73 | + */ |
| 74 | + public function init_customizer_options() { |
| 75 | + |
| 76 | + $cfs = $this->customizer->styles(); |
| 77 | + |
| 78 | + // Inject hyperlink style |
| 79 | + $hyperlink_color = get_theme_mod( $this->prefix( 'hyperlink-color' ) ); |
| 80 | + $hyperlink_underline = get_theme_mod( $this->prefix( 'hyperlink-underline' ) ) ? 'underline' : 'none'; |
| 81 | + |
| 82 | + $cfs->register([ |
| 83 | + 'a' // CSS selector |
| 84 | + ], |
| 85 | + [ |
| 86 | + "color: {$hyperlink_color}", |
| 87 | + "text-decoration: {$hyperlink_underline}", |
| 88 | + ] |
| 89 | + //,'@media (min-width: 768px)' // Optional |
| 90 | + ); |
| 91 | + |
| 92 | + // Inject H2 color style |
| 93 | + $h2_color = get_theme_mod( $this->prefix( 'h2-color' ) ); |
| 94 | + |
| 95 | + $cfs->register([ |
| 96 | + 'h2' // CSS selector |
| 97 | + ], |
| 98 | + [ |
| 99 | + "color: {$h2_color}" |
| 100 | + ] |
| 101 | + ); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Add relevant Customizer options to page head |
| 107 | + * @since 0.4.0 |
| 108 | + */ |
| 109 | + public function inject_customizer_options() { |
| 110 | + |
| 111 | + $favicon = get_theme_mod( $this->prefix( 'favicon' ) ); |
| 112 | + if( !$favicon ) return; |
| 113 | + $file_type = wp_check_filetype( $favicon ); |
| 114 | + |
| 115 | + echo sprintf('<link rel="icon" href="%s" type="%s" />', $favicon, $file_type['type'] ); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments