PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce-product-navigation/woocommerce-product-navigation.php

https://gitlab.com/hunt9310/ras
PHP | 260 lines | 152 code | 51 blank | 57 comment | 53 complexity | ad9532d67152e458540a9ede68298013 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: WooCommerce Product Navigation
  4. Plugin URI: http://wpbackoffice.com/plugins/woocommerce-product-navigation
  5. Description: Easily enable users to navigate from one product to the next with our next / previous product buttons. Simply activate the plugin and you're done! Then visit the settings page to upload custom images, change the position and format text.
  6. Version: 1.0.0
  7. Author: WP BackOffice
  8. Author URI: http://wpbackoffice.com
  9. License: GPLv2 or later
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  12. if ( ! class_exists( 'Woocommerce_Product_Navigation' ) ) :
  13. class Woocommerce_Product_Navigation {
  14. public $options = array();
  15. public $button_positions = array(
  16. 'before_product' => 'woocommerce_before_single_product',
  17. 'before_summary' => 'woocommerce_before_single_product_summary',
  18. 'product_summary' => 'woocommerce_single_product_summary',
  19. 'after_product' => 'woocommerce_after_single_product',
  20. 'after_summary' => 'woocommerce_after_single_product_summary'
  21. );
  22. public $defaults = array (
  23. 'wpn_button_type' => 'product_name',
  24. 'wpn_next_text' => 'Next Product',
  25. 'wpn_next_id' => '',
  26. 'wpn_previous_text' => 'Previous Product',
  27. 'wpn_previous_id' => '',
  28. 'wpn_position' => 'product_summary',
  29. 'wpn_position_priority' => 10,
  30. 'wpn_custom_class' => '',
  31. 'wpn_display_buttons' => ''
  32. );
  33. public $button_position = 'woocommerce_single_product_summary';
  34. public $button_priority = 10;
  35. public function __construct() {
  36. // Retreive plugin options
  37. $this->options = get_option( 'wpn_options' );
  38. // Activation Hook
  39. register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
  40. // Include Required Files
  41. require_once( 'wpn-settings.php' );
  42. // Add front-end styles
  43. add_action( 'wp_enqueue_scripts', array( $this, 'add_styles' ) );
  44. // Get the desired positon of buttons from settings
  45. $this->set_button_position();
  46. $this->set_button_priority();
  47. if ( isset( $this->options['wpn_display_buttons'] ) and $this->options['wpn_display_buttons'] != 'on' ) {
  48. // Add Button to Single Product Action
  49. add_action( $this->button_position, array( $this, 'product_navigation_buttons' ), $this->button_priority );
  50. }
  51. // Add Shortcode
  52. add_shortcode( 'product_navigation', array( $this, 'product_navigation_buttons' ) );
  53. // Add Plugin Settings
  54. $plugin = plugin_basename(__FILE__);
  55. add_filter("plugin_action_links_$plugin", array( $this, 'wpn_settings_links' ) );
  56. // Clear Options (for testing)
  57. //delete_option('wpn_options');
  58. }
  59. /*
  60. * Add settings link on plugin page
  61. */
  62. public function wpn_settings_links($links) {
  63. $support_link = '<a href="http://www.wpbackoffice.com">Premium Support</a>';
  64. array_unshift($links, $support_link);
  65. $docs_link = '<a href="http://www.wpbackoffice.com/plugins/woocommerce-product-navigation">Docs</a>';
  66. array_unshift($links, $docs_link);
  67. $settings_link = '<a href="/wp-admin/admin.php?page=wpn-settings.php">Settings</a>';
  68. array_unshift($links, $settings_link);
  69. return $links;
  70. }
  71. /*
  72. * Adds default option values
  73. */
  74. public function activation_hook() {
  75. $options = get_option( 'wpn_options' );
  76. if ( $options == false or $options == '' ) {
  77. $result = add_option( 'wpn_options', $this->defaults );
  78. } else {
  79. foreach ( $this->defaults as $key => $val ) {
  80. if ( !isset( $options[$key] ) ) {
  81. $options[$key] = $val;
  82. }
  83. }
  84. update_option( 'wpn_options', $options );
  85. }
  86. }
  87. /*
  88. * Include Styles
  89. */
  90. public function add_styles() {
  91. global $woocommerce;
  92. if ( is_product() ) {
  93. wp_enqueue_style(
  94. 'wpn_product_styles',
  95. plugins_url( '/assets/css/wpn-product.css', __FILE__ )
  96. );
  97. }
  98. }
  99. /*
  100. * Show the next / previous buttons on the single product page
  101. */
  102. public function product_navigation_buttons() {
  103. if ( $this->options == false ) {
  104. return;
  105. }
  106. extract( $this->options );
  107. // Get the current url
  108. global $post;
  109. $current_url = get_permalink( $post->ID );
  110. $next = '';
  111. $previous = '';
  112. // Get the previous and next product links
  113. // $previous_link = get_permalink(get_adjacent_post(false,'',false));
  114. // $next_link = get_permalink(get_adjacent_post(false,'',true));
  115. $$next_link = get_permalink( get_adjacent_post(false,'',false));
  116. $previous_link = get_permalink( get_adjacent_post(false,'',true));
  117. // Congigure text button type
  118. if ( $wpn_button_type == 'text' ) {
  119. // Get previous text if it exists, otherwise use default
  120. if ( isset( $wpn_previous_text ) and $wpn_previous_text != '' ) {
  121. $previous_text = $wpn_previous_text;
  122. } else {
  123. $previous_text = 'Previous Product';
  124. }
  125. // Get next text if it exists, otherwise use default
  126. if ( isset( $wpn_next_text ) and $wpn_next_text != '' ) {
  127. $next_text = $wpn_next_text;
  128. } else {
  129. $next_text = 'Next Product';
  130. }
  131. // Create the two links provided the product exists
  132. if ( $next_link != $current_url ) {
  133. $next = "<a href='" . $next_link . "'>" . $next_text . "</a>";
  134. }
  135. if ( $previous_link != $current_url ) {
  136. $previous = "<a href='" . $previous_link . "'>" . $previous_text . "</a>";
  137. }
  138. // Configure image button type
  139. } else if ( $wpn_button_type == 'image' ) {
  140. // If image id exists proceed, otherwise return
  141. if ( isset( $wpn_previous_id ) and $wpn_previous_id != '' ) {
  142. $previous_url = wp_get_attachment_url( $wpn_previous_id );
  143. } else {
  144. return;
  145. }
  146. // If image id exists proceed, otherwise return
  147. if ( isset( $wpn_next_id ) and $wpn_next_id != '' ) {
  148. $next_url = wp_get_attachment_url( $wpn_next_id );
  149. } else {
  150. return;
  151. }
  152. // Create the two links provided the product exists
  153. if ( $next_link != $current_url ) {
  154. $next = "<a href='" . $next_link . "'><img src='" . $next_url . "'></a>";
  155. }
  156. if ( $previous_link != $current_url ) {
  157. $previous = "<a href='" . $previous_link . "'><img src='" . $previous_url . "'></a>";
  158. }
  159. // Show basic product name
  160. } else if ( $wpn_button_type == 'product_name' ) {
  161. // Create the two links provided the product exists
  162. if ( $next_link != $current_url ) {
  163. $next_text = get_adjacent_post(false,'',true)->post_title;
  164. $next = "<a href='" . $next_link . "'>" . $next_text . "</a>";
  165. }
  166. if ( $previous_link != $current_url ) {
  167. $previous_text = get_adjacent_post(false,'',false)->post_title;
  168. $previous = "<a href='" . $previous_link . "'>" . $previous_text . "</a>";
  169. }
  170. // Otherwise the setting doesn't validate so return
  171. } else {
  172. return;
  173. }
  174. // Create HTML Output
  175. $output = '<div class="wpn_buttons ' . $wpn_custom_class .'">';
  176. if ( $previous != '' )
  177. $output .= '<span class="previous"> ' . $previous . '</span>';
  178. if ( $next != '' )
  179. $output .= '<span class="next">' . $next .'</span>';
  180. $output .= '</div>';
  181. // Display the final output
  182. echo $output;
  183. }
  184. /*
  185. * Returns the correct woocommerce hook for displaying the buttons
  186. */
  187. public function set_button_position() {
  188. if ( isset( $this->options['wpn_position'] ) and isset( $this->button_positions[$this->options['wpn_position']] ) ) {
  189. $this->button_position = $this->button_positions[$this->options['wpn_position']];
  190. } else {
  191. $this->button_position = 'woocommerce_after_single_product_summary';
  192. }
  193. }
  194. /*
  195. * Returns the button hook priority
  196. */
  197. public function set_button_priority() {
  198. if ( isset( $this->options['wpn_position_priority'] ) and
  199. $this->options['wpn_position_priority'] != '' and
  200. $this->options['wpn_position_priority'] > 0 ) {
  201. $this->button_priority = $this->options['wpn_position_priority'];
  202. }
  203. }
  204. }
  205. endif;
  206. /*
  207. * Create a new instance of the plugin
  208. */
  209. new Woocommerce_Product_Navigation();