Wcb_WooCommerce_Breadcrumbs_plugin

Plugin Name: WooCommerce Breadcrumbs Plugin URI: http://maddisondesigns.com/woocommerce-breadcrumbs Description: A simple plugin to style the WooCommerce Breadcrumbs or disable them altogether Version: 1.0.2 Author: Anthony Hortin Author URI: http://maddisondesigns.com Text Domain: woocommerce-breadcrumbs License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html.

Defined (1)

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

/woocommerce-breadcrumbs.php  
  1. class Wcb_WooCommerce_Breadcrumbs_plugin { 
  2.  
  3. private $options; 
  4. private $breadcrumb_defaults; 
  5. private $wootheme_theme; 
  6.  
  7. public function __construct() { 
  8.  
  9. $this->breadcrumb_defaults = array( 
  10. 'wcb_enable_breadcrumbs' => 1,  
  11. 'wcb_breadcrumb_delimiter' => ' / ',  
  12. 'wcb_wrap_before' => '<nav class="woocommerce-breadcrumb">',  
  13. 'wcb_wrap_after' => '</nav>',  
  14. 'wcb_before' => '',  
  15. 'wcb_after' => '',  
  16. 'wcb_home_text' => _x( 'Home', 'breadcrumb', 'woocommerce-breadcrumbs' ),  
  17. 'wcb_home_url' => esc_url( home_url( '/' ) ) 
  18. ); 
  19.  
  20. add_action( 'admin_menu', array( $this, 'wcb_create_menu_option' ) ); 
  21. add_action( 'admin_init', array( $this, 'wcb_admin_init' ) ); 
  22. add_action( 'init', array( $this, 'wcb_init' ) ); 
  23. add_filter( 'plugin_action_links', array( $this, 'wcb_add_settings_link'), 10, 2); 
  24. add_action( 'head', 'woocommerce_breadcrumb', 20, 0); 
  25.  
  26. $this->options = ( get_option( 'wcb_breadcrumb_options' ) === false ? $this->breadcrumb_defaults : get_option( 'wcb_breadcrumb_options' ) ); 
  27.  
  28. if( empty( $this->options['wcb_enable_breadcrumbs'] ) ) { 
  29. add_action( 'init', array( $this, 'wcb_remove_woocommerce_breadcrumb' ) ); 
  30.  
  31. /** 
  32. * Add a new option to the Settings menu 
  33. */ 
  34. public function wcb_create_menu_option() { 
  35. add_options_page( 'WooCommerce Breadcrumbs', 'WC Breadcrumbs', 'manage_options', 'woocommerce-breadcrumbs', array( $this, 'wcb_plugin_settings_page' ) ); 
  36.  
  37. /** 
  38. * Add a settings link to plugin page 
  39. */ 
  40. public function wcb_add_settings_link( $links, $file ) { 
  41. static $this_plugin; 
  42.  
  43. if( !$this_plugin ) { 
  44. $this_plugin = plugin_basename( __FILE__ ); 
  45.  
  46. if( $file == $this_plugin ) { 
  47. $settings_link = '<a href="options-general.php?page=woocommerce-breadcrumbs">' . __( 'Settings', 'woocommerce-breadcrumbs' ) . '</a>'; 
  48. array_unshift( $links, $settings_link ) ; 
  49.  
  50. return $links; 
  51.  
  52. /** 
  53. * Create our settings page 
  54. */ 
  55. public function wcb_plugin_settings_page() { 
  56. $this->options = ( get_option( 'wcb_breadcrumb_options' ) === false ? $this->breadcrumb_defaults : get_option( 'wcb_breadcrumb_options' ) ); 
  57.  
  58. if( !is_plugin_active( 'woocommerce/woocommerce.php' ) ) { 
  59. $message = __('It appears that WooCommerce is not currently activated. To get the most out of WooCommerce Breadcrumbs install & activate the WooCommerce plugin', 'woocommerce-breadcrumbs' ); 
  60. add_settings_error( 'woocommerce-breadcrumb-warnings', esc_attr( 'wcb_woocommerce_disabled' ), $message, 'error' ); 
  61.  
  62. if ( $this->wootheme_theme ) { 
  63. $message = esc_html("It looks like you're using a WooThemes theme. If you notice a few less breadcrumb options than you may expect this is because WooThemes disables the WooCommerce breadcrumbs in favour of the WooFramework Breadcrumbs.", 'woocommerce-breadcrumbs' ); 
  64. add_settings_error( 'woocommerce-breadcrumb-warnings', esc_attr( 'wcb_woo_framework_breadcrumbs' ), $message, 'updated' ); 
  65.  
  66. settings_errors( 'woocommerce-breadcrumb-warnings' ); 
  67.  
  68. echo '<div class="wrap">'; 
  69. screen_icon(); 
  70. echo '<h2>WooCommerce Breadcrumbs</h2>'; 
  71. echo '<form action="options.php" method="post">'; 
  72. settings_fields( 'wcb_breadcrumb_options' ); 
  73. do_settings_sections( 'woocommerce-breadcrumbs' ); 
  74. echo '<p>'; 
  75. submit_button( _x( 'Save Changes', 'breadcrumb', 'woocommerce-breadcrumbs' ), 'primary', 'submit', false ); 
  76. $other_attributes = array ( 
  77. 'onclick' => "return confirm( '" . esc_html__( 'Click OK to reset to the default breadcrumb settings!', 'woocommerce-breadcrumbs' ) . "' );" 
  78. ); 
  79. submit_button( 'Restore Defaults', 'secondary alignright', 'restore_defaults', false, $other_attributes ); 
  80. echo '</p>'; 
  81. echo '</form>'; 
  82. echo '</div>'; 
  83.  
  84.  
  85. /** 
  86. * Register and define the settings 
  87. */ 
  88. public function wcb_admin_init() { 
  89. register_setting( 'wcb_breadcrumb_options', 'wcb_breadcrumb_options', array( $this, 'wcb_plugin_sanitize_options' ) ); 
  90. add_settings_section( 'wcb_general_settings', 'Breadcrumb Settings', array( $this, 'wcb_plugin_section_callback' ), 'woocommerce-breadcrumbs' ); 
  91. add_settings_field( 'wcb_enable_breadcrumbs', 'Enable breadcrumbs', array( $this, 'wcb_enable_breadcrumbs_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  92. add_settings_field( 'wcb_breadcrumb_delimiter', 'Breadcrumb separator', array( $this, 'wcb_breadcrumb_delimiter_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  93. add_settings_field( 'wcb_wrap_before', 'Wrap before', array( $this, 'wcb_wrap_before_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  94. add_settings_field( 'wcb_wrap_after', 'Wrap after', array( $this, 'wcb_wrap_after_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  95. if ( !$this->wootheme_theme ) { 
  96. // We can't set these if the theme is using the WooFramework Breadcrumbs instead of the WooCommerce Breadcrumbs, so don't show them 
  97. add_settings_field( 'wcb_before', 'Before', array( $this, 'wcb_before_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  98. add_settings_field( 'wcb_after', 'After', array( $this, 'wcb_after_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  99. add_settings_field( 'wcb_home_text', 'Home text', array( $this, 'wcb_home_text_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  100. if ( !$this->wootheme_theme ) { 
  101. // We can't set this if the theme is using the WooFramework Breadcrumbs instead of the WooCommerce Breadcrumbs, so don't show it 
  102. add_settings_field( 'wcb_home_url', 'Home URL', array( $this, 'wcb_home_url_callback' ), 'woocommerce-breadcrumbs', 'wcb_general_settings' ); 
  103.  
  104. /** 
  105. * Once the theme is initialised, check to see if it's a WooTheme theme as they typically disable WooCommerce breadcrumbs 
  106. * in favour of the WooFramework Breadcrumbs (which behave a little differently) 
  107. * Set the breadcrumbs now that we have all the details 
  108. */ 
  109. public function wcb_init() { 
  110. if ( function_exists( 'woo_breadcrumbs' ) ) { 
  111. remove_filter( 'woo_breadcrumbs_args', 'woo_custom_breadcrumbs_args', 10 ); 
  112. $this->breadcrumb_defaults['wcb_breadcrumb_delimiter'] = '>'; 
  113. $this->breadcrumb_defaults['wcb_wrap_before'] = '<span class="breadcrumb-title">' . __( 'You are here:', 'woocommerce-breadcrumbs' ) . '</span>'; 
  114. $this->breadcrumb_defaults['wcb_wrap_after'] = ''; 
  115. $this->wootheme_theme = true; 
  116. else { 
  117. $this->wootheme_theme = false; 
  118.  
  119. if( !empty( $this->options['wcb_enable_breadcrumbs'] ) ) { 
  120. if ( $this->wootheme_theme ) { 
  121. add_filter( 'woo_breadcrumbs_args', array( $this, 'wcb_woocommerce_set_breadcrumbs' ), 11 ); 
  122. else { 
  123. add_filter( 'woocommerce_breadcrumb_defaults', array( $this, 'wcb_woocommerce_set_breadcrumbs' ) ); 
  124. add_filter( 'woocommerce_breadcrumb_home_url', array( $this, 'wcb_woocommerce_breadcrumb_home_url' ) ); 
  125.  
  126. /** 
  127. * Display a section message 
  128. */ 
  129. public function wcb_plugin_section_callback() { 
  130. printf( '<p>%s</p>', __( 'Customise the look of your WooCommerce breadcrumbs, using the settings below. Alternatively, disable them altogether by unchecking ‘Enable breadcrumbs’.', 'woocommerce-breadcrumbs' ) ); 
  131.  
  132. /** 
  133. * Display and fill the form field for the delimeter setting 
  134. */ 
  135. function wcb_enable_breadcrumbs_callback() { 
  136. $enable_breadcrumbs = ( isset( $this->options['wcb_enable_breadcrumbs'] ) ? $this->options['wcb_enable_breadcrumbs'] : '0' ); 
  137.  
  138. printf( '<input id="wcb_enable_breadcrumbs" type="checkbox" name="wcb_breadcrumb_options[wcb_enable_breadcrumbs]" value="%1$s" %2$s/>',  
  139. $enable_breadcrumbs,  
  140. checked( $enable_breadcrumbs, true, false ) ); 
  141.  
  142. /** 
  143. * Display and fill the form field for the delimeter setting 
  144. */ 
  145. public function wcb_breadcrumb_delimiter_callback() { 
  146. $breadcrumb_delimiter = ( isset( $this->options['wcb_breadcrumb_delimiter'] ) ? $this->options['wcb_breadcrumb_delimiter'] : '' ); 
  147.  
  148. printf( '<input id="wcb_breadcrumb_delimiter" class="regular-text" name="wcb_breadcrumb_options[wcb_breadcrumb_delimiter]" type="text" value="%s"/>',  
  149. $breadcrumb_delimiter ); 
  150. printf( '<p class="description">%s</p>', __( 'This is the separator to use between each breadcrumb.', 'woocommerce-breadcrumbs' ) ); 
  151.  
  152. /** 
  153. * Display and fill the form field for the Wrap Before setting 
  154. */ 
  155. public function wcb_wrap_before_callback() { 
  156. $wrap_before = ( isset( $this->options['wcb_wrap_before'] ) ? $this->options['wcb_wrap_before'] : '' ); 
  157.  
  158. printf( '<input id="wcb_wrap_before" class="regular-text" name="wcb_breadcrumb_options[wcb_wrap_before]" type="text" value="%s"/>',  
  159. esc_attr( $wrap_before ) ); 
  160. if ( $this->wootheme_theme ) { 
  161. $msg = __( 'The opening html tag to display before all your breadcrumbs.', 'woocommerce-breadcrumbs' ); 
  162. else { 
  163. $msg = __( 'The opening html tag to wrap before all your breadcrumbs.', 'woocommerce-breadcrumbs' ); 
  164. printf( '<p class="description">%s</p>', $msg ); 
  165.  
  166. /** 
  167. * Display and fill the form field for the Wrap After setting 
  168. */ 
  169. public function wcb_wrap_after_callback() { 
  170. $wrap_after = ( isset( $this->options['wcb_wrap_after'] ) ? $this->options['wcb_wrap_after'] : '' ); 
  171.  
  172. printf( '<input id="wcb_wrap_after" class="regular-text" name="wcb_breadcrumb_options[wcb_wrap_after]" type="text" value="%s"/>',  
  173. esc_attr( $wrap_after ) ); 
  174. if ( $this->wootheme_theme ) { 
  175. $msg = __( 'The closing html tag to display after all your breadcrumbs.', 'woocommerce-breadcrumbs' ); 
  176. else { 
  177. $msg = __( 'The closing html tag to wrap after all your breadcrumbs.', 'woocommerce-breadcrumbs' ); 
  178. printf( '<p class="description">%s</p>', $msg ); 
  179.  
  180. /** 
  181. * Display and fill the form field for the Before setting 
  182. */ 
  183. public function wcb_before_callback() { 
  184. $before = ( isset( $this->options['wcb_before'] ) ? $this->options['wcb_before'] : '' ); 
  185.  
  186. printf( '<input id="wcb_before" class="regular-text" name="wcb_breadcrumb_options[wcb_before]" type="text" value="%s"/>',  
  187. esc_attr( $before ) ); 
  188. printf( '<p class="description">%s</p>', __( 'The opening html tag to wrap before each individual breadcrumb.', 'woocommerce-breadcrumbs' ) ); 
  189.  
  190. /** 
  191. * Display and fill the form field for the After setting 
  192. */ 
  193. public function wcb_after_callback() { 
  194. $after = ( isset( $this->options['wcb_after'] ) ? $this->options['wcb_after'] : '' ); 
  195.  
  196. printf( '<input id="wcb_after" class="regular-text" name="wcb_breadcrumb_options[wcb_after]" type="text" value="%s"/>',  
  197. esc_attr( $after ) ); 
  198. printf( '<p class="description">%s</p>', __( 'The closing html tag to wrap after each individual breadcrumb.', 'woocommerce-breadcrumbs' ) ); 
  199.  
  200. /** 
  201. * Display and fill the form field for the Home Text setting 
  202. */ 
  203. public function wcb_home_text_callback() { 
  204. $home_text = ( isset( $this->options['wcb_home_text'] ) ? $this->options['wcb_home_text'] : '' ); 
  205.  
  206. printf( '<input id="wcb_home_text" class="regular-text" name="wcb_breadcrumb_options[wcb_home_text]" type="text" value="%s"/>',  
  207. $home_text ); 
  208. printf( '<p class="description">%s</p>', __( 'The text to use for the ‘Home’ breadcrumb.', 'woocommerce-breadcrumbs' ) ); 
  209.  
  210. /** 
  211. * Display and fill the form field for the Home URL setting 
  212. */ 
  213. public function wcb_home_url_callback() { 
  214. $home_url = ( isset( $this->options['wcb_home_url'] ) ? $this->options['wcb_home_url'] : '' ); 
  215.  
  216. printf( '<input id="wcb_home_url" class="regular-text" name="wcb_breadcrumb_options[wcb_home_url]" type="text" value="%s"/>',  
  217. esc_attr( $home_url ) ); 
  218. printf( '<p class="description">%s</p>', __( 'The URL that the ‘Home’ breadcrumb links to.', 'woocommerce-breadcrumbs' ) ); 
  219.  
  220. /** 
  221. * Validate and sanitize each of our options 
  222. */ 
  223. public function wcb_plugin_sanitize_options( $input ) { 
  224. $valid = array(); 
  225.  
  226. // If the Restore Defaults button is clicked, reset the values to the default values 
  227. if ( isset( $_POST['restore_defaults'] ) ) { 
  228. $message = esc_html('Default options restored', 'woocommerce-breadcrumbs' ); 
  229. add_settings_error( 'woocommerce-breadcrumbs', esc_attr( 'wcb_restore_defaults' ), $message, 'updated' ); 
  230. return $this->breadcrumb_defaults; 
  231.  
  232. // Validate the inputs 
  233. $valid['wcb_enable_breadcrumbs'] = ( isset( $input['wcb_enable_breadcrumbs'] ) ? '1' : '0' ); 
  234.  
  235. $valid['wcb_breadcrumb_delimiter'] = wp_kses_data( $input['wcb_breadcrumb_delimiter'] ); 
  236.  
  237. $valid['wcb_wrap_before'] = wp_kses_post( $input['wcb_wrap_before'] ); 
  238.  
  239. $valid['wcb_wrap_after'] = wp_kses_post( $input['wcb_wrap_after'] ); 
  240.  
  241. $valid['wcb_before'] = wp_kses_post( $input['wcb_before'] ); 
  242.  
  243. $valid['wcb_after'] = wp_kses_post( $input['wcb_after'] ); 
  244.  
  245. $valid['wcb_home_text'] = sanitize_text_field( $input['wcb_home_text'] ); 
  246.  
  247. $valid['wcb_home_url'] = esc_url( $input['wcb_home_url'] ); 
  248.  
  249. return $valid; 
  250.  
  251. /** 
  252. * Remove the WooCommerce Breadcrumbs 
  253. */ 
  254. public function wcb_remove_woocommerce_breadcrumb() { 
  255. if ( $this->wootheme_theme ) { 
  256. remove_filter( 'woo_main_before', 'woo_display_breadcrumbs', 10 ); 
  257. else { 
  258. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 ); 
  259.  
  260. /** 
  261. * Change the Home link for the Breadrumbs 
  262. */ 
  263. public function wcb_woocommerce_breadcrumb_home_url() { 
  264. return $this->options['wcb_home_url']; 
  265.  
  266. /** 
  267. * Set the breadcrumbs 
  268. */ 
  269. public function wcb_woocommerce_set_breadcrumbs() { 
  270.  
  271. if ( $this->wootheme_theme ) { 
  272. return array( 
  273. 'separator' => $this->options['wcb_breadcrumb_delimiter'],  
  274. 'before' => $this->options['wcb_wrap_before'],  
  275. 'after' => $this->options['wcb_wrap_after'],  
  276. 'show_home' => _x( $this->options['wcb_home_text'], 'breadcrumb', 'woocommerce-breadcrumbs' ) 
  277. ); 
  278. else { 
  279. return array( 
  280. 'delimiter' => $this->options['wcb_breadcrumb_delimiter'],  
  281. 'wrap_before' => $this->options['wcb_wrap_before'],  
  282. 'wrap_after' => $this->options['wcb_wrap_after'],  
  283. 'before' => $this->options['wcb_before'],  
  284. 'after' => $this->options['wcb_after'],  
  285. 'home' => _x( $this->options['wcb_home_text'], 'breadcrumb', 'woocommerce-breadcrumbs' ) 
  286. );