PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/custom-headers/custom-headers.php

https://gitlab.com/axminenko/rocks-tools
PHP | 310 lines | 172 code | 48 blank | 90 comment | 27 complexity | c2330e37294cd1235f45db9a1bffe2ec MD5 | raw file
  1. <?php
  2. /**
  3. * Create.Rocks Tools
  4. * A poweful plugin to extend functionality to your WordPress themes offering shortcodes, font icons and useful widgets.
  5. *
  6. * @package Create_Rocks_Tools
  7. * @author Create.Rocks Team <support@create.rocks>
  8. * @copyright 2014 - 2016 Create.Rocks
  9. * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
  10. * @version 0.1.0
  11. * @link http://create.rocks/plugin/tools
  12. */
  13. /**
  14. * Custom headers
  15. *
  16. * @package Create_Rocks_Tools
  17. * @subpackage Custom_Headers
  18. */
  19. class Rocks_Tools_Custom_Headers {
  20. /**
  21. * Constructor
  22. *
  23. * @access public
  24. */
  25. function __construct( ) {
  26. if ( ! is_dir( get_template_directory( ) . '/components/custom-headers' ) ) {
  27. return;
  28. }
  29. //add_action( 'add_meta_boxes', array( &$this, 'metabox_init' ) );
  30. //add_action( 'save_post', array( &$this, 'metabox_save' ) );
  31. add_action( 'customize_register', array( &$this, 'customize_register' ) );
  32. }
  33. /**
  34. * Initialization
  35. *
  36. * @return Rocks_Tools_Custom_Headers
  37. * @access public
  38. * @static
  39. */
  40. public static function init( ) {
  41. return new self( );
  42. }
  43. /**
  44. * Customize register
  45. *
  46. * @param WP_Customize $wp_customize
  47. * @access public
  48. *
  49. * @global Rocks_Tools $rocks_tools
  50. */
  51. public function customize_register( $wp_customize ) {
  52. global $rocks_tools;
  53. // Custom controls for Customize API
  54. require plugin_dir_path( __FILE__ ) . 'customize-controls.php';
  55. $wp_customize->add_section( 'header', array(
  56. 'title' => __( 'Headers', 'rocks' ),
  57. 'priority' => 30,
  58. ) );
  59. // Header Layout
  60. $wp_customize->add_setting( 'header_layout', array(
  61. 'default' => 'header-1',
  62. ) );
  63. $wp_customize->add_control( new Rocks_Tools_Customize_Header_Control( $wp_customize, 'header_layout', array(
  64. 'label' => __( 'Header Layout', 'rocks' ),
  65. 'section' => 'header',
  66. 'priority' => 10,
  67. ) ) );
  68. // Mobile Header Layout
  69. if ( $rocks_tools->get_option( 'custom-mobile-headers', false ) ) {
  70. $wp_customize->add_setting( 'mobile_header_layout', array(
  71. 'default' => 'mobile-1',
  72. ) );
  73. $wp_customize->add_control( new Rocks_Tools_Customize_Mobile_Header_Control( $wp_customize, 'mobile_header_layout', array(
  74. 'label' => __( 'Mobile Header Layout', 'rocks' ),
  75. 'section' => 'header',
  76. 'priority' => 20,
  77. ) ) );
  78. }
  79. // Footer Layout
  80. if ( $rocks_tools->get_option( 'custom-footers', false ) ) {
  81. $wp_customize->add_section( 'footer', array(
  82. 'title' => __( 'Footers', 'rocks' ),
  83. 'priority' => 90,
  84. ) );
  85. $wp_customize->add_setting( 'footer_layout', array(
  86. 'default' => 'footer-1',
  87. ) );
  88. $wp_customize->add_control( new Rocks_Tools_Customize_Footer_Control( $wp_customize, 'footer_layout', array(
  89. 'label' => __( 'Footer Layout', 'rocks' ),
  90. 'section' => 'footer',
  91. 'priority' => 10,
  92. ) ) );
  93. }
  94. }
  95. /**
  96. * Register needed scripts and styles
  97. *
  98. * @access public
  99. * @static
  100. *
  101. * @global Rocks_Tools $rocks_tools
  102. */
  103. public static function load_scripts_styles( ) {
  104. global $rocks_tools;
  105. wp_enqueue_style( 'rocks-tools-custom-headers', plugins_url( 'css/custom-headers.min.css', __FILE__ ), array( ), $rocks_tools->get_version( ) );
  106. wp_enqueue_script( 'rocks-tools-custom-headers', plugins_url( 'js/custom-headers.min.js', __FILE__ ), array( 'jquery' ), $rocks_tools->get_version( ), true );
  107. }
  108. /**
  109. * Initialization custom header metabox
  110. *
  111. * @access public
  112. *
  113. * @global Rocks_Tools $rocks_tools
  114. */
  115. public function metabox_init( ) {
  116. global $rocks_tools;
  117. $title = __( 'Custom Header & Footer', 'rocks' );
  118. // Custom footer support
  119. if ( ! $rocks_tools->get_option( 'custom-footers', false ) ) {
  120. $title = __( 'Custom Header', 'rocks' );
  121. }
  122. // Pages
  123. if ( current_user_can( 'edit_pages' ) ) {
  124. add_meta_box( 'rocks_tools_custom_header', $title, array( &$this, 'metabox_content' ), 'page', 'side' );
  125. }
  126. // Posts
  127. if ( current_user_can( 'edit_posts' ) ) {
  128. add_meta_box( 'rocks_tools_custom_header', $title, array( &$this, 'metabox_content' ), 'post', 'side' );
  129. }
  130. }
  131. /**
  132. * Get files list
  133. *
  134. * @param string $directory
  135. * @access public
  136. * @static
  137. */
  138. public static function get_files_list( $directory = 'normal' ) {
  139. $files = array( );
  140. $path = get_template_directory( ) . '/components/custom-headers/' . $directory;
  141. if ( ! is_dir( $path ) ) {
  142. return false;
  143. }
  144. $list = scandir( $path );
  145. if ( ! is_array( $list ) ) {
  146. return false;
  147. }
  148. foreach ( $list as $name ) {
  149. if ( $name == '.' or $name == '..' ) {
  150. continue;
  151. }
  152. $files[] = substr( $name, 0, strrpos( $name, '.' ) );
  153. }
  154. if ( count( $files ) > 0 ) {
  155. return $files;
  156. }
  157. return false;
  158. }
  159. /**
  160. * Get formatted name
  161. *
  162. * @param string $name
  163. * @return string
  164. * @access public
  165. */
  166. public function get_formatted_name( $name ) {
  167. return ucwords( str_replace( array( '-', '_' ), ' ', $name ) );
  168. }
  169. /**
  170. * Metabox content
  171. *
  172. * @param WP_Post $post
  173. * @access public
  174. *
  175. * @global Rocks_Tools $rocks_tools
  176. */
  177. public function metabox_content( $post ) {
  178. global $rocks_tools;
  179. self::load_scripts_styles( );
  180. $list_headers = $list_footers = '';
  181. $headers = $footers = array( );
  182. $allow_footers = $rocks_tools->get_option( 'custom-footers', false );
  183. $current_header = get_post_meta( $post->ID, 'rocks_tools_custom_header_id', true );
  184. $current_footer = get_post_meta( $post->ID, 'rocks_tools_custom_footer_id', true );
  185. $previews = $this->get_files_list( );
  186. if ( $previews !== false ) {
  187. foreach ( $previews as $name ) {
  188. $list_headers .= '<li data-id="' . esc_attr( $name ) . '"' . ( $name == $current_header ? ' class="checked"' : '' ) . '><img src="' . get_template_directory_uri( ) . '/components/custom-headers/normal/' . esc_attr( $name ) . '.png"></li>';
  189. }
  190. wp_nonce_field( 'rocks_tools_custom_headers_nonce', 'rocks_tools_custom_headers_nonce_safe' );
  191. echo '
  192. <div class="rocks_tools_custom_headers_option title">
  193. <input type="hidden" name="rocks_tools_custom_header_id" value="' . esc_attr( $current_header ) . '">
  194. <strong>
  195. ' . __( 'Header', 'rocks' ) . ' &ndash;
  196. <span>' . ( empty( $current_header ) ? __( 'Default', 'rocks' ) : __( 'Custom', 'rocks' ) ) . '</span>
  197. </strong>
  198. <i class="fa fa-angle-down"></i>
  199. </div>
  200. <div class="rocks_tools_custom_headers_option block">
  201. <input class="button rocks_tools_custom_headers_reset" value="' . __( 'Set Defaults', 'rocks' ) . '" type="button">
  202. <ul class="rocks_tools_custom_headers_previews">' . $list_headers . '</ul>
  203. </div>';
  204. }
  205. if ( $allow_footers ) {
  206. $previews = $this->get_files_list( 'footers' );
  207. if ( $previews !== false ) {
  208. foreach ( $previews as $name ) {
  209. $list_footers .= '<li data-id="' . esc_attr( $name ) . '"' . ( $name == $current_footer ? ' class="checked"' : '' ) . '><img src="' . get_template_directory_uri( ) . '/components/custom-headers/footers/' . esc_attr( $name ) . '.png"></li>';
  210. }
  211. echo '
  212. <div class="rocks_tools_custom_headers_option title">
  213. <input type="hidden" name="rocks_tools_custom_footer_id" value="' . esc_attr( $current_footer ) . '">
  214. <strong>
  215. ' . __( 'Footer', 'rocks' ) . ' &ndash;
  216. <span>' . ( empty( $current_footer ) ? __( 'Default', 'rocks' ) : __( 'Custom', 'rocks' ) ) . '</span>
  217. </strong>
  218. <i class="fa fa-angle-down"></i>
  219. </div>
  220. <div class="rocks_tools_custom_headers_option block">
  221. <input class="button rocks_tools_custom_headers_reset" value="' . __( 'Set Defaults', 'rocks' ) . '" type="button">
  222. <ul class="rocks_tools_custom_headers_previews">' . $list_footers . '</ul>
  223. </div>';
  224. }
  225. }
  226. $howto = $allow_footers ? __( 'You can change defaults header and footer on the <a href="%s" target="_blank">customization page</a>.', 'rocks' ) : __( 'You can change default header on the <a href="%s" target="_blank">customization page</a>.', 'rocks' );
  227. echo '<p class="howto rocks_tools_custom_header_howto">' . sprintf( $howto, 'customize.php' ) . '</p>';
  228. }
  229. /**
  230. * Update metabox details
  231. *
  232. * @param int $post_id
  233. * @access public
  234. */
  235. public function metabox_save( $post_id ) {
  236. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  237. return;
  238. }
  239. if ( ! isset( $_POST['rocks_tools_custom_headers_nonce_safe'] ) or ! wp_verify_nonce( $_POST['rocks_tools_custom_headers_nonce_safe'], 'rocks_tools_custom_headers_nonce' ) ) {
  240. return;
  241. }
  242. if ( ! empty( $_POST['post_type'] ) and $_POST['post_type'] == 'page' ) {
  243. if ( ! current_user_can( 'edit_pages' ) ) {
  244. return;
  245. }
  246. } else {
  247. if ( ! current_user_can( 'edit_posts' ) ) {
  248. return;
  249. }
  250. }
  251. if ( isset( $_POST['rocks_tools_custom_header_id'] ) ) {
  252. update_post_meta( $post_id, 'rocks_tools_custom_header_id', sanitize_text_field( $_POST['rocks_tools_custom_header_id'] ) );
  253. }
  254. if ( isset( $_POST['rocks_tools_custom_footer_id'] ) ) {
  255. update_post_meta( $post_id, 'rocks_tools_custom_footer_id', sanitize_text_field( $_POST['rocks_tools_custom_footer_id'] ) );
  256. }
  257. }
  258. }
  259. // Initialization
  260. add_action( 'rocks_tools_loaded', array( 'Rocks_Tools_Custom_Headers', 'init' ) );