Site_Logo

Our Site Logo class for managing a theme-agnostic logo through the Customizer.

Defined (1)

The class is defined in the following location(s).

/modules/theme-tools/site-logo/inc/class-site-logo.php  
  1. class Site_Logo { 
  2. /** 
  3. * Stores our single instance. 
  4. */ 
  5. private static $instance; 
  6.  
  7. /** 
  8. * Stores our current logo settings. 
  9. */ 
  10. public $logo; 
  11.  
  12. /** 
  13. * Return our instance, creating a new one if necessary. 
  14. * @uses Site_Logo::$instance 
  15. * @return object Site_Logo 
  16. */ 
  17. public static function instance() { 
  18. if ( ! isset( self::$instance ) ) { 
  19. self::$instance = new Site_Logo; 
  20. self::$instance->register_hooks(); 
  21.  
  22. return self::$instance; 
  23.  
  24. /** 
  25. * Get our current logo settings stored in options. 
  26. * @uses get_option() 
  27. */ 
  28. private function __construct() { 
  29. $this->logo = get_option( 'site_logo', null ); 
  30.  
  31. /** 
  32. * Register our actions and filters. 
  33. * @uses Site_Logo::head_text_styles() 
  34. * @uses Site_Logo::customize_register() 
  35. * @uses Site_Logo::preview_enqueue() 
  36. * @uses Site_Logo::body_classes() 
  37. * @uses Site_Logo::media_manager_image_sizes() 
  38. * @uses add_action 
  39. * @uses add_filter 
  40. */ 
  41. public function register_hooks() { 
  42. add_action( 'wp_head', array( $this, 'head_text_styles' ) ); 
  43. add_action( 'customize_register', array( $this, 'customize_register' ) ); 
  44. add_action( 'customize_preview_init', array( $this, 'preview_enqueue' ) ); 
  45. add_action( 'delete_attachment', array( $this, 'reset_on_attachment_delete' ) ); 
  46. add_filter( 'body_class', array( $this, 'body_classes' ) ); 
  47. add_filter( 'image_size_names_choose', array( $this, 'media_manager_image_sizes' ) ); 
  48. add_filter( 'display_media_states', array( $this, 'add_media_state' ) ); 
  49.  
  50. /** 
  51. * Add our logo uploader to the Customizer. 
  52. * @param object $wp_customize Customizer object. 
  53. * @uses current_theme_supports() 
  54. * @uses current_theme_supports() 
  55. * @uses WP_Customize_Manager::add_setting() 
  56. * @uses WP_Customize_Manager::add_control() 
  57. * @uses Site_Logo::sanitize_checkbox() 
  58. */ 
  59. public function customize_register( $wp_customize ) { 
  60. // Include our custom control. 
  61. require( dirname( __FILE__ ) . '/class-site-logo-control.php' ); 
  62.  
  63. //Update the Customizer section title for discoverability. 
  64. $wp_customize->get_section('title_tagline')->title = __( 'Site Title, Tagline, and Logo', 'jetpack' ); 
  65.  
  66. // Add a setting to hide header text if the theme isn't supporting the feature itself 
  67. if ( ! current_theme_supports( 'custom-header' ) ) { 
  68. $wp_customize->add_setting( 'site_logo_header_text', array( 
  69. 'default' => 1,  
  70. 'sanitize_callback' => array( $this, 'sanitize_checkbox' ),  
  71. 'transport' => 'postMessage',  
  72. ) ); 
  73.  
  74. $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'site_logo_header_text', array( 
  75. 'label' => __( 'Display Header Text', 'jetpack' ),  
  76. 'section' => 'title_tagline',  
  77. 'settings' => 'site_logo_header_text',  
  78. 'type' => 'checkbox',  
  79. ) ) ); 
  80.  
  81. // Add the setting for our logo value. 
  82. $wp_customize->add_setting( 'site_logo', array( 
  83. 'capability' => 'manage_options',  
  84. 'default' => array( 
  85. 'id' => 0,  
  86. 'sizes' => array(),  
  87. 'url' => false,  
  88. ),  
  89. 'sanitize_callback' => array( $this, 'sanitize_logo_setting' ),  
  90. 'transport' => 'postMessage',  
  91. 'type' => 'option',  
  92. ) ); 
  93.  
  94. // Add our image uploader. 
  95. $wp_customize->add_control( new Site_Logo_Image_Control( $wp_customize, 'site_logo', array( 
  96. 'label' => __( 'Logo', 'jetpack' ),  
  97. 'section' => 'title_tagline',  
  98. 'settings' => 'site_logo',  
  99. ) ) ); 
  100.  
  101. /** 
  102. * Enqueue scripts for the Customizer live preview. 
  103. * @uses wp_enqueue_script() 
  104. * @uses plugins_url() 
  105. * @uses current_theme_supports() 
  106. * @uses Site_Logo::header_text_classes() 
  107. * @uses wp_localize_script() 
  108. */ 
  109. public function preview_enqueue() { 
  110. wp_enqueue_script( 'site-logo-preview', plugins_url( '../js/site-logo.js', __FILE__ ), array( 'media-views' ), '', true ); 
  111.  
  112. // Don't bother passing in header text classes if the theme supports custom headers. 
  113. if ( ! current_theme_supports( 'custom-header' ) ) { 
  114. $classes = jetpack_sanitize_header_text_classes( $this->header_text_classes() ); 
  115. wp_enqueue_script( 'site-logo-header-text', plugins_url( '../js/site-logo-header-text.js', __FILE__ ), array( 'media-views' ), '', true ); 
  116. wp_localize_script( 'site-logo-header-text', 'site_logo_header_classes', $classes ); 
  117.  
  118. /** 
  119. * Get header text classes. If not defined in add_theme_support(), defaults from Underscores will be used. 
  120. * @uses get_theme_support 
  121. * @return string String of classes to hide 
  122. */ 
  123. public function header_text_classes() { 
  124. $args = get_theme_support( 'site-logo' ); 
  125.  
  126. if ( isset( $args[0][ 'header-text' ] ) ) { 
  127. // Use any classes defined in add_theme_support(). 
  128. $classes = $args[0][ 'header-text' ]; 
  129. } else { 
  130. // Otherwise, use these defaults, which will work with any Underscores-based theme. 
  131. $classes = array( 
  132. 'site-title',  
  133. 'site-description',  
  134. ); 
  135.  
  136. // If we've got an array, reduce them to a string for output 
  137. if ( is_array( $classes ) ) { 
  138. $classes = (string) '.' . implode( ', .', $classes ); 
  139. } else { 
  140. $classes = (string) '.' . $classes; 
  141.  
  142. return $classes; 
  143.  
  144. /** 
  145. * Hide header text on front-end if necessary. 
  146. * @uses current_theme_supports() 
  147. * @uses get_theme_mod() 
  148. * @uses Site_Logo::header_text_classes() 
  149. * @uses esc_html() 
  150. */ 
  151. public function head_text_styles() { 
  152. // Bail if our theme supports custom headers. 
  153. if ( current_theme_supports( 'custom-header' ) ) { 
  154. return; 
  155.  
  156. // Is Display Header Text unchecked? If so, we need to hide our header text. 
  157. if ( ! get_theme_mod( 'site_logo_header_text', 1 ) ) { 
  158. $classes = $this->header_text_classes(); 
  159. ?> 
  160. <!-- Site Logo: hide header text --> 
  161. <style type="text/css"> 
  162. <?php echo jetpack_sanitize_header_text_classes( $classes ); ?> { 
  163. position: absolute; 
  164. clip: rect(1px, 1px, 1px, 1px); 
  165. </style> 
  166. <?php 
  167.  
  168. /** 
  169. * Determine image size to use for the logo. 
  170. * @uses get_theme_support() 
  171. * @return string Size specified in add_theme_support declaration, or 'thumbnail' default 
  172. */ 
  173. public function theme_size() { 
  174. $args = get_theme_support( 'site-logo' ); 
  175. $valid_sizes = get_intermediate_image_sizes(); 
  176.  
  177. // Add 'full' to the list of accepted values. 
  178. $valid_sizes[] = 'full'; 
  179.  
  180. // If the size declared in add_theme_support is valid, use it; otherwise, just go with 'thumbnail'. 
  181. $size = ( isset( $args[0]['size'] ) && in_array( $args[0]['size'], $valid_sizes ) ) ? $args[0]['size'] : 'thumbnail'; 
  182.  
  183. return $size; 
  184.  
  185. /** 
  186. * Make custom image sizes available to the media manager. 
  187. * @param array $sizes 
  188. * @uses get_intermediate_image_sizes() 
  189. * @return array All default and registered custom image sizes. 
  190. */ 
  191. public function media_manager_image_sizes( $sizes ) { 
  192. // Get an array of all registered image sizes. 
  193. $intermediate = get_intermediate_image_sizes(); 
  194.  
  195. // Have we got anything fun to work with? 
  196. if ( is_array( $intermediate ) && ! empty( $intermediate ) ) { 
  197. foreach ( $intermediate as $key => $size ) { 
  198. // If the size isn't already in the $sizes array, add it. 
  199. if ( ! array_key_exists( $size, $sizes ) ) { 
  200. $sizes[ $size ] = $size; 
  201.  
  202. return $sizes; 
  203.  
  204. /** 
  205. * Add site logos to media states in the Media Manager. 
  206. * @return array The current attachment's media states. 
  207. */ 
  208. public function add_media_state( $media_states ) { 
  209. // Only bother testing if we have a site logo set. 
  210. if ( $this->has_site_logo() ) { 
  211. global $post; 
  212.  
  213. // If our attachment ID and the site logo ID match, this image is the site logo. 
  214. if ( $post->ID == $this->logo['id'] ) { 
  215. $media_states[] = __( 'Site Logo', 'jetpack' ); 
  216.  
  217. return $media_states; 
  218.  
  219. /** 
  220. * Reset the site logo if the current logo is deleted in the media manager. 
  221. * @param int $site_id 
  222. * @uses Site_Logo::remove_site_logo() 
  223. */ 
  224. public function reset_on_attachment_delete( $post_id ) { 
  225. if ( $this->logo['id'] == $post_id ) { 
  226. $this->remove_site_logo(); 
  227.  
  228. /** 
  229. * Determine if a site logo is assigned or not. 
  230. * @uses Site_Logo::$logo 
  231. * @return boolean True if there is an active logo, false otherwise 
  232. */ 
  233. public function has_site_logo() { 
  234. return ( isset( $this->logo['id'] ) && 0 !== $this->logo['id'] ) ? true : false; 
  235.  
  236. /** 
  237. * Reset the site logo option to zero (empty). 
  238. * @uses update_option() 
  239. */ 
  240. public function remove_site_logo() { 
  241. update_option( 'site_logo', array( 
  242. 'id' => (int) 0,  
  243. 'sizes' => array(),  
  244. 'url' => '',  
  245. ) ); 
  246.  
  247. /** 
  248. * Adds custom classes to the array of body classes. 
  249. * @uses Site_Logo::has_site_logo() 
  250. * @return array Array of <body> classes 
  251. */ 
  252. public function body_classes( $classes ) { 
  253. // Add a class if a Site Logo is active 
  254. if ( $this->has_site_logo() ) { 
  255. $classes[] = 'has-site-logo'; 
  256.  
  257. return $classes; 
  258.  
  259. /** 
  260. * Sanitize our header text Customizer setting. 
  261. * @param $input 
  262. * @return mixed 1 if checked, empty string if not checked. 
  263. */ 
  264. public function sanitize_checkbox( $input ) { 
  265. return ( 1 == $input ) ? 1 : ''; 
  266.  
  267. /** 
  268. * Validate and sanitize a new site logo setting. 
  269. * @param $input 
  270. * @return mixed 1 if checked, empty string if not checked. 
  271. */ 
  272. public function sanitize_logo_setting( $input ) { 
  273. $input['id'] = absint( $input['id'] ); 
  274. $input['url'] = esc_url_raw( $input['url'] ); 
  275.  
  276. // If the new setting doesn't point to a valid attachment, just reset the whole thing. 
  277. if ( false == wp_get_attachment_image_src( $input['id'] ) ) { 
  278. $input = array( 
  279. 'id' => (int) 0,  
  280. 'sizes' => array(),  
  281. 'url' => '',  
  282. ); 
  283.  
  284. return $input;