Wccal

Configuration page.

Defined (1)

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

/woocommerce-cloak-affiliate-links.php  
  1. class Wccal { 
  2.  
  3. public function __construct() { 
  4.  
  5. register_activation_hook( __FILE__, array( $this, 'activate_plugin' ) ); 
  6. register_deactivation_hook( __FILE__, array( $this, 'deactivate_plugin' ) ); 
  7.  
  8. $this->base = self::get_affiliate_base(); 
  9. $this->options = $this->load_options(); 
  10.  
  11. add_filter( 'query_vars', array( $this, 'query_vars' ) ); 
  12. add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules_array' ) ); 
  13. add_filter( 'woocommerce_product_class', array( $this, 'woocommerce_product_class' ), 40, 4 ); 
  14. add_filter( 'robots_txt', array( $this, 'robots_txt' ), 10, 2 ); 
  15. add_filter( 'plugin_action_links_' . WCCAL_BASENAME, array( $this, 'action_links' ) ); 
  16.  
  17. add_action( 'template_redirect', array( $this, 'template_redirect' ) ); 
  18. add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); 
  19. add_action( 'admin_init', array( $this, 'permalink_settings_init' ) ); 
  20. add_action( 'admin_init', array( $this, 'permalink_settings_save' ) ); 
  21. add_action( 'admin_menu', array( $this, 'options_page' ) ); 
  22. add_action( 'admin_init', array( $this, 'register_settings' ) ); 
  23. add_action( 'wccal_clickthrough', array( $this, 'count_clickthrough' ) ); 
  24.  
  25. /** 
  26. * Flush rerwrite rules on plugin activation. 
  27. */  
  28. function activate_plugin() { 
  29. flush_rewrite_rules(); 
  30.  
  31. /** 
  32. * Flush rerwrite rules on plugin deactivation. 
  33. */  
  34. function deactivate_plugin() { 
  35. flush_rewrite_rules(); 
  36.  
  37. /** 
  38. * Add "permalink" path to robots.txt file. 
  39. */ 
  40. function robots_txt( $output, $public ) { 
  41. if ( $this->options['robots'] == 'yes' && get_option('permalink_structure') ) { 
  42. $site_url = parse_url( site_url() ); 
  43. $path = ( !empty( $site_url['path'] ) ) ? $site_url['path'] : ''; 
  44. $text = "Disallow: $path/" . $this->base . "/\n"; 
  45. $text = apply_filters( 'wccal_robots_txt', $text ); 
  46. $output .= $text; 
  47. return $output; 
  48.  
  49. /** 
  50. * Get the base permalink settings. 
  51. */ 
  52. static public function get_affiliate_base() { 
  53. $permalinks = get_option( 'wccal_permalinks' ); 
  54. if ( ! $permalinks || !isset( $permalinks['affiliate_base'] ) || $permalinks['affiliate_base'] == '' ) { 
  55. return 'redirect'; 
  56. return $permalinks['affiliate_base']; 
  57.  
  58. /** 
  59. * Set default option values. 
  60. */ 
  61. function default_options() { 
  62. return array( 
  63. 'status' => '302',  
  64. 'robots' => 'yes',  
  65. ); 
  66.  
  67. /** 
  68. * Load default or configured options. 
  69. */ 
  70. function load_options() { 
  71. $options = array_merge(  
  72. $this->default_options(),  
  73. get_option( 'wccal_options', array() )  
  74. ); 
  75. update_option( 'wccal_options', $options ); 
  76. return $options; 
  77.  
  78. /** 
  79. * Add "Settings" page to "Settings" menu. 
  80. */ 
  81. function options_page() {  
  82. add_options_page(  
  83. __( 'WooCommerce Cloak Affiliate Links Settings', WCCAL_DOMAIN ),  
  84. __( 'WC Cloak Links', WCCAL_DOMAIN ),  
  85. 'manage_options',  
  86. 'wccal-options',  
  87. array( $this, 'build_options_page' ) 
  88. ); 
  89.  
  90. /** 
  91. * Add "Settings" link to plugin page. 
  92. */ 
  93. function action_links( $links ) { 
  94. return array_merge( 
  95. array( 
  96. 'settings' => '<a href="' . admin_url( 'options-general.php?page=wccal-options' ) . '">' . __( 'Settings', WCCAL_DOMAIN ) . '</a>',  
  97. ),  
  98. $links 
  99. ); 
  100.  
  101. /** 
  102. * Set up the options page to configure the WCCAL plugin. 
  103. */ 
  104. function build_options_page() { 
  105. echo '<div class="wrap" id="wccal_options">'; 
  106. echo '<h2>' . __( 'WooCommerce Cloak Affiliate Links Settings', WCCAL_DOMAIN ) . '</h2>'; 
  107. echo '<form method="post" action="options.php">'; 
  108. wp_nonce_field( 'update-wccal-options' ); 
  109. settings_fields( 'wccal-options' ); 
  110. do_settings_sections( 'wccal-options' ); 
  111. submit_button(); 
  112. echo '</form>';  
  113. echo '</div>'; 
  114.  
  115. /** 
  116. * Register settings. 
  117. */ 
  118. function register_settings() { 
  119. register_setting( 'wccal-options', 'wccal_options', array( $this, 'validate' ) ); 
  120. add_settings_section( 'general_settings', __( 'General Settings', WCCAL_DOMAIN ), array( &$this, 'section_general_settings_desc' ), 'wccal-options' ); 
  121. add_settings_field( 'status', __( 'Status Code', WCCAL_DOMAIN ), array( &$this, 'field_status' ), 'wccal-options', 'general_settings' ); 
  122. if ( get_option('permalink_structure') ) { 
  123. add_settings_field( 'robots', __( 'Add Redirect Path to Robots.txt', WCCAL_DOMAIN ), array( &$this, 'field_robots' ), 'wccal-options', 'general_settings' ); 
  124.  
  125. /** 
  126. * General settings decription. 
  127. */ 
  128. function section_general_settings_desc() { 
  129. // _e( 'General plugin settings.', WCCAL_DOMAIN ); 
  130.  
  131. /** 
  132. * Field to select Status Code. 
  133. */ 
  134. function field_status() { ?> 
  135. <select id="wwcal_status" name="wccal_options[status]"> 
  136. <option value="301" <?php selected( $this->options['status'], '301', true ); ?>><?php _e( '301 (Moved Permanently)', WCCAL_DOMAIN ); ?></option> 
  137. <option value="302" <?php selected( $this->options['status'], '302', true ); ?>><?php _e( '302 (Found/Temporary Redirect)', WCCAL_DOMAIN ); ?></option> 
  138. <option value="307" <?php selected( $this->options['status'], '307', true ); ?>><?php _e( '307 (Temporary Redirect)', WCCAL_DOMAIN ); ?></option> 
  139. </select> 
  140. <p class="description"><?php _e( 'The status code to use when performing the redirect', WCCAL_DOMAIN ); ?></p> 
  141. <?php 
  142.  
  143. /** 
  144. * Field to enabled/disable robots.txt. 
  145. */ 
  146. function field_robots() { ?> 
  147. <p><input type="radio" value="yes" name="wccal_options[robots]" <?php checked( $this->options['robots'], 'yes', true ); ?> /> <?php _e( 'Yes', WCCAL_DOMAIN ); ?></p> 
  148. <p><input type="radio" value="no" name="wccal_options[robots]" <?php checked( $this->options['robots'], 'no', true ); ?> /> <?php _e( 'No', WCCAL_DOMAIN ); ?></p> 
  149. <p class="description"><?php _e( 'Add the path configured on your Permalinks page to your robots.txt file to prevent any search engines from attempting to view or index that path.', WCCAL_DOMAIN ); ?></p> 
  150. <?php 
  151.  
  152. /** 
  153. * Register a new var. 
  154. */ 
  155. function query_vars( $vars) { 
  156. $vars[] = $this->base; 
  157. return $vars; 
  158.  
  159. /** 
  160. * Add the new rewrite rule to existings ones. 
  161. */ 
  162. function rewrite_rules_array( $rules ) { 
  163. $new_rules = array( $this->base . '/([^/]+)/?$' => 'index.php?' . $this->base . '=$matches[1]' ); 
  164. $rules = $new_rules + $rules; 
  165. return $rules; 
  166.  
  167. /** 
  168. * Redirect the user to external link. 
  169. */ 
  170. function template_redirect() { 
  171.  
  172. global $wp_query; 
  173.  
  174. if ( isset( $wp_query->query_vars[$this->base] ) ) { 
  175.  
  176. $post_id = intval( get_query_var( $this->base ) ); 
  177. $external_link = get_post_meta( $post_id, '_product_url', true ); 
  178. $external_link = apply_filters( 'wccal_filter_url', $external_link, $post_id ); 
  179. $external_link = trim( $external_link ); 
  180.  
  181. if ( $external_link != '' ) {  
  182. $url = $external_link; 
  183. do_action( 'wccal_clickthrough', $post_id ); 
  184. } else { 
  185. $url = get_permalink( $post_id ); 
  186. do_action( 'wccal_clickthrough_fail', $post_id ); 
  187.  
  188. wp_redirect( $url, $this->options['status'] ); 
  189. exit(); 
  190.  
  191. /** 
  192. * Add 1 to clickthrough count. 
  193. */ 
  194. function count_clickthrough( $post_id ) { 
  195. $count = intval( get_post_meta( $post_id, '_wccal_clickthrough_count', true ) ); 
  196. update_post_meta( $post_id, '_wccal_clickthrough_count', ( $count + 1 ) ); 
  197.  
  198. /** 
  199. * Change "WC_Product_External" class to our own class if class is "WC_Product_External". 
  200. */ 
  201. function woocommerce_product_class( $classname, $product_type, $post_type, $product_id ) { 
  202. return ( is_admin() || $classname != 'WC_Product_External' ) ? $classname : 'Wccal_Product_External'; 
  203.  
  204. /** 
  205. * This loads our class only after all plugins have loaded. 
  206. */ 
  207. function plugins_loaded() { 
  208. if ( ! class_exists( 'WC_Product_External' ) ) { return; }  
  209. require_once( WCCAL_PATH . 'class-wccal-product-external.php' ); 
  210.  
  211. /** 
  212. * permalink_settings_init function. 
  213. */ 
  214. function permalink_settings_init() { 
  215. add_settings_field( 
  216. 'wccal_redirect_slug',  
  217. __( 'Affiliate link base', WCCAL_DOMAIN ),  
  218. array( $this, 'permalink_input' ),  
  219. 'permalink',  
  220. 'optional' 
  221. ); 
  222.  
  223. /** 
  224. * permalink_input function. 
  225. */ 
  226. function permalink_input() { 
  227. $permalinks = get_option( 'wccal_permalinks' ); 
  228. ?> 
  229. <input name="wccal_affiliate_base" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['affiliate_base'] ) ) echo esc_attr( $permalinks['affiliate_base'] ); ?>" placeholder="<?php echo _x( 'redirect', 'slug', WCCAL_DOMAIN ); ?>" /><code>/%post_id%/</code> 
  230. <?php 
  231.  
  232. /** 
  233. * permalink_settings_save function. 
  234. */ 
  235. function permalink_settings_save() { 
  236. if ( ! is_admin() ) { return; } 
  237. if ( isset( $_POST['wccal_affiliate_base'] ) ) { 
  238. $wccal_affiliate_base = wc_clean( $_POST['wccal_affiliate_base'] ); 
  239. $permalinks = get_option( 'wccal_permalinks' ); 
  240. if ( ! $permalinks ) { 
  241. $permalinks = array(); 
  242. $permalinks['affiliate_base'] = untrailingslashit( $wccal_affiliate_base ); 
  243. update_option( 'wccal_permalinks', $permalinks ); 
  244.  
  245. /** 
  246. * Validate options submitted. 
  247. */ 
  248. function validate( $input ) {  
  249. if ( !isset( $input ) || !is_array( $input ) || empty( $input ) ) { return $input; } 
  250. $new_input = array(); 
  251. foreach( $input as $key => $value ) { 
  252. if ( $key == 'status' ) { $new_input['status'] = $value; }  
  253. if ( $key == 'robots' ) { $new_input['robots'] = $value; }  
  254. return $new_input; 
  255.  
  256.  
  257. } // class Wccal