-
Notifications
You must be signed in to change notification settings - Fork 3
/
sku-shortlink-for-woocommerce.php
174 lines (147 loc) · 4.84 KB
/
sku-shortlink-for-woocommerce.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Plugin Name: SKU Shortlink For WooCommerce
* Plugin URI: https://wordpress.org/plugins/SKU-Shortlink-For-WooCommerce/
* Description: SKU Shortlink For WooCommerce
* Version: 0.2
* Author: Varun Sridharan
* Author URI: http://varunsridharan.in
* Text Domain: sku-shortlink-for-woocommerce
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: @TODO
*/
if ( ! defined( 'WPINC' ) ) { die; }
class SKU_Shortlink_For_WooCommerce {
/**
* @var string
*/
public $version = '0.2';
/**
* @var WooCommerce The single instance of the class
* @since 2.1
*/
protected static $_instance = null;
protected static $functions = null;
public $url_types = null;
/**
* Creates or returns an instance of this class.
*/
public static function get_instance() {
if ( null == self::$_instance ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Class Constructor
*/
public function __construct() {
$link = get_option('woocommerce_permalinks',true);
$plink = trailingslashit($link['product_base']);
$this->url_types = array(0 => $plink.'sku/%sku%/', 1 => $plink.'%sku%/', 2 => '%sku%/', 'custom' => 'custom link');
$this->define_constant();
$this->load_required_files();
$this->init_class();
add_action('plugins_loaded', array( $this, 'after_plugins_loaded' ));
add_filter('load_textdomain_mofile', array( $this, 'load_plugin_mo_files' ), 10, 2);
}
/**
* Loads Required Plugins For Plugin
*/
private function load_required_files(){
$this->load_files(SKU_SF_WC_PATH.'includes/class-frontend.php');
if($this->is_request('admin')){
$this->load_files(SKU_SF_WC_PATH.'admin/class-*.php');
}
}
/**
* Inits loaded Class
*/
private function init_class(){
$this->frontend = new SKU_Shortlink_For_WooCommerce_Frontend;
if($this->is_request('admin')){
$this->admin = new SKU_Shortlink_For_WooCommerce_Admin;
}
}
protected function load_files($path,$type = 'require'){
foreach( glob( $path ) as $files ){
if($type == 'require'){
require_once( $files );
} else if($type == 'include'){
include_once( $files );
}
}
}
/**
* Set Plugin Text Domain
*/
public function after_plugins_loaded(){
load_plugin_textdomain(SKU_SF_WC_TEXT_DOMAIN, false, SKU_SF_WC_LANGUAGE_PATH );
}
/**
* load translated mo file based on wp settings
*/
public function load_plugin_mo_files($mofile, $domain) {
if (SKU_SF_WC_TEXT_DOMAIN === $domain)
return SKU_SF_WC_LANGUAGE_PATH.'/'.get_locale().'.mo';
return $mofile;
}
/**
* Define Required Constant
*/
private function define_constant(){
$this->define('SKU_SF_WC_NAME','SKU Shortlink For WooCommerce'); # Plugin Name
$this->define('SKU_SF_WC_SLUG','sku_sf_wc'); # Plugin Slug
$this->define('SKU_SF_WC_PATH',plugin_dir_path( __FILE__ )); # Plugin DIR
$this->define('SKU_SF_WC_LANGUAGE_PATH',SKU_SF_WC_PATH.'languages');
$this->define('SKU_SF_WC_TEXT_DOMAIN','sku-shortlink-for-woocommerce'); #plugin lang Domain
$this->define('SKU_SF_WC_URL',plugins_url('', __FILE__ ));
$this->define('SKU_SF_WC_FILE',plugin_basename( __FILE__ ));
$this->define('SKU_SF_WC_VERSION', $this->version);
}
/**
* Define constant if not already set
* @param string $name
* @param string|bool $value
*/
protected function define($key,$value){
if(!defined($key)){
define($key,$value);
}
}
/**
* Adds Filter / Action
*/
protected function add_filter_action($key,$value,$type = 'action' , $priority = 10, $variable = 1){
if($type == 'action'){
add_action($key,$value,$priority,$variable);
} else if($type == 'filter'){
add_filter($key,$value,$priority,$variable);
} else {
return false;
}
}
/**
* What type of request is this?
* string $type ajax, frontend or admin
* @return bool
*/
private function is_request( $type ) {
switch ( $type ) {
case 'admin' :
return is_admin();
case 'ajax' :
return defined( 'DOING_AJAX' );
case 'cron' :
return defined( 'DOING_CRON' );
case 'frontend' :
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
}
}
SKU_Shortlink_For_WooCommerce::get_instance();
function SKUSFWC(){
return SKU_Shortlink_For_WooCommerce::get_instance();
}
?>