-
Notifications
You must be signed in to change notification settings - Fork 0
/
edd-per-product-button.php
executable file
·217 lines (184 loc) · 6.5 KB
/
edd-per-product-button.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Plugin Name: EDD Per Product Button
* Plugin URI: https://www.wphuman.com
* Description: Setup custom button text for each product.
* Version: 1.0.0
* Author: WP Human
* Author URI: https://www.wphuman.com
* Text Domain: edd-per-product-button
*
* @package EDD\PerProductButton
* @author WP Human
* @copyright Copyright (c) WP Human
*
*/
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;
if( !class_exists( 'EDD_Per_Product_Button' ) ) {
/**
* Main EDD_Per_Product_Button class
*
* @since 1.0.0
*/
class EDD_Per_Product_Button {
/**
* @var EDD_Per_Product_Button $instance The one true EDD_Per_Product_Button
* @since 1.0.0
*/
private static $instance;
/**
* Get active instance
*
* @access public
* @since 1.0.0
* @return object self::$instance The one true EDD_Per_Product_Button
*/
public static function instance() {
if( !self::$instance ) {
self::$instance = new EDD_Per_Product_Button();
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->load_textdomain();
self::$instance->hooks();
}
return self::$instance;
}
/**
* Setup plugin constants
*
* @access private
* @since 1.0.0
* @return void
*/
private function setup_constants() {
// Plugin version
define( 'EDD_PER_PRODUCT_BUTTON_VER', '1.0.0' );
// Plugin path
define( 'EDD_PER_PRODUCT_BUTTON_DIR', plugin_dir_path( __FILE__ ) );
// Plugin URL
define( 'EDD_PER_PRODUCT_BUTTON_URL', plugin_dir_url( __FILE__ ) );
}
/**
* Include necessary files
*
* @access private
* @since 1.0.0
* @return void
*/
private function includes() {
require_once EDD_PER_PRODUCT_BUTTON_DIR . 'includes/class-per-product-button-public.php';
require_once EDD_PER_PRODUCT_BUTTON_DIR . 'includes/class-per-product-button-admin.php';
}
/**
* Run action and filter hooks
*
* @access private
* @since 1.0.0
* @return void
*
*/
private function hooks() {
// Register settings
add_filter( 'edd_settings_extensions', array( $this, 'settings' ), 1 );
// Handle licensing
// @todo Replace the Per Product Button and Your Name with your data
if( class_exists( 'EDD_License' ) ) {
$license = new EDD_License( __FILE__, 'Per Product Button', EDD_PER_PRODUCT_BUTTON_VER, 'WP Human' );
}
// Public class
$public = new EDD_Per_Product_Button_Public();
add_filter( 'edd_purchase_link_defaults', array( $public, 'purchase_link_defaults' ) );
add_filter( 'edd_purchase_link_args', array( $public, 'purchase_link_args' ), 5000 );
// Admin class
$admin = new EDD_Per_Product_Button_Admin();
add_action( 'edd_meta_box_fields', array( $admin, 'meta_box_fields' ), 100 );
add_filter( 'edd_metabox_fields_save', array( $admin, 'metabox_fields_save' ) );
add_filter( 'edd_metabox_save__edd_per_product_button_text', array( $admin, 'sanitize_button_text' ) );
add_filter( 'edd_metabox_save__edd_per_product_button_force_override', array( $admin, 'sanitize_button_force_override' ) );
add_filter( 'edd_metabox_save__edd_per_product_button_style', array( $admin, 'sanitize_button_style' ) );
add_filter( 'edd_metabox_save__edd_per_product_button_color', array( $admin, 'sanitize_button_color' ) );
}
/**
* Internationalization
*
* @access public
* @since 1.0.0
* @return void
*/
public function load_textdomain() {
// Set filter for language directory
$lang_dir = EDD_PER_PRODUCT_BUTTON_DIR . '/languages/';
$lang_dir = apply_filters( 'edd_per_product_button_languages_directory', $lang_dir );
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), 'edd-per-product-button' );
$mofile = sprintf( '%1$s-%2$s.mo', 'edd-per-product-button', $locale );
// Setup paths to current locale file
$mofile_local = $lang_dir . $mofile;
$mofile_global = WP_LANG_DIR . '/edd-per-product-button/' . $mofile;
if( file_exists( $mofile_global ) ) {
// Look in global /wp-content/languages/edd-per-product-button/ folder
load_textdomain( 'edd-per-product-button', $mofile_global );
} elseif( file_exists( $mofile_local ) ) {
// Look in local /wp-content/plugins/edd-per-product-button/languages/ folder
load_textdomain( 'edd-per-product-button', $mofile_local );
} else {
// Load the default language files
load_plugin_textdomain( 'edd-per-product-button', false, $lang_dir );
}
}
/**
* Add settings
*
* @access public
* @since 1.0.0
* @param array $settings The existing EDD settings array
* @return array The modified EDD settings array
*/
public function settings( $settings ) {
$new_settings = array(
array(
'id' => 'edd_per_product_button_settings',
'name' => '<strong>' . __( 'Per Product Button Settings', 'edd-per-product-button' ) . '</strong>',
'desc' => __( 'Configure Per Product Button Settings', 'edd-per-product-button' ),
'type' => 'header',
)
);
return array_merge( $settings, $new_settings );
}
}
} // End if class_exists check
/**
* The main function responsible for returning the one true EDD_Per_Product_Button
* instance to functions everywhere
*
* @since 1.0.0
* @return \EDD_Per_Product_Button The one true EDD_Per_Product_Button
*
*/
function EDD_Per_Product_Button_load() {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) {
if( ! class_exists( 'EDD_Extension_Activation' ) ) {
require_once 'includes/class.extension-activation.php';
}
$activation = new EDD_Extension_Activation( plugin_dir_path( __FILE__ ), basename( __FILE__ ) );
$activation = $activation->run();
return EDD_Per_Product_Button::instance();
} else {
return EDD_Per_Product_Button::instance();
}
}
add_action( 'plugins_loaded', 'EDD_Per_Product_Button_load' );
/**
* The activation hook is called outside of the singleton because WordPress doesn't
* register the call from within the class, since we are preferring the plugins_loaded
* hook for compatibility, we also can't reference a function inside the plugin class
* for the activation function. If you need an activation function, put it here.
*
* @since 1.0.0
* @return void
*/
function edd_per_product_button_activation() {
/* Activation functions here */
}
register_activation_hook( __FILE__, 'edd_per_product_button_activation' );