-
Notifications
You must be signed in to change notification settings - Fork 96
/
functions.php
302 lines (227 loc) · 8.39 KB
/
functions.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
// Load Framework //
require_once( dirname(__FILE__) . '/setup.php' );
class baFotosTheme {
const version = '1.0';
function __construct() {
// Constants
$this->url = sprintf('%s', PL_CHILD_URL);
$this->dir = sprintf('/%s', PL_CHILD_DIR);
// Includes
include('inc/contact.php');
include('inc/unset.php');
include('inc/support-tab.php');
include('inc/options.php');
include('inc/post.php');
include('inc/partials.php');
include('inc/gallery.php');
include('inc/fotos-shortcodes.php');
define( 'BA_FOTOS_STORE_URL', 'http://nickhaskins.co' );
define( 'BA_FOTOS_THEME_NAME', 'Fotos' );
// Load Updates API
if ( !class_exists( 'EDD_SL_Theme_Updater' ) ) {
include( dirname( __FILE__ ) . '/EDD_SL_Theme_Updater.php' );
}
// hide acf UI
if(!defined('FOTOS_DEV')) {
if (!defined('ACF_LITE')) {
define( 'ACF_LITE' , true );
}
}
// load acf
if ( !class_exists( 'Acf' ) ) {
include_once('libs/advanced-custom-fields/acf.php' );
}
// load acf gallery
if ( !function_exists( 'acfgp_register_fields' ) ){
include_once('libs/advanced-custom-fields/add-ons/acf-gallery/acf-gallery.php');
}
if(!defined('FOTOS_DEV'))
require_once('inc/acf-register.php');
$this->init();
}
// Initialize
function init() {
// bypass posix check
add_filter( 'render_css_posix_', '__return_true' );
// i18n
add_action( 'after_setup_theme', array($this,'fotos_textdomain' ));
// Add Microdata
add_action('the_html_tag', array($this,'fotos_microdata'));
// Fotos Utilities
add_action( 'wp_enqueue_scripts',array($this,'scripts'));
// Load Widgets
add_action( 'widgets_init', array($this,'load_widgets') );
// Get license Key
$license = trim( get_option( 'ba_fotos_license_key' ) );
// Pass to Updater
$edd_updater = new EDD_SL_Theme_Updater( array(
'remote_api_url' => BA_FOTOS_STORE_URL,
'version' => self::version,
'license' => $license,
'item_name' => BA_FOTOS_THEME_NAME,
'author' => 'Nick Haskins'
)
);
// Updater Filters
add_action('admin_menu', array($this,'license_menu'));
add_action('admin_init', array($this,'register_option'));
add_action('admin_init', array($this,'activate_license'));
add_action('admin_init', array($this,'deactivate_license'));
}
function fotos_textdomain() {
load_child_theme_textdomain( 'fotos-textdomain', $this->dir. '/lang' );
}
function fotos_microdata(){
$base = 'http://schema.org/';
if( is_page('contact') ){
$type = 'ContactPage';
}
elseif( is_page('about') ){
$type = 'AboutPage';
}
elseif( is_author() ){
$type = 'ProfilePage';
}
elseif( is_search() ){
$type = 'SearchResultsPage';
}
else {
$type = 'WebPage';
}
echo ' itemscope="itemscope" itemtype="' . $base . $type . '"';
}
// Load Widgets
function load_widgets() {
// Load Widget Lib Files
require_once('widgets/custom-search.php');
// Register Widgets
register_widget('Fotos_Custom_Search');
}
function scripts(){
wp_register_script('fotos', PL_CHILD_URL.'/assets/js/fotos.min.js', array('jquery'), self::version, true);
wp_enqueue_script('fotos');
}
function license_menu() {
add_theme_page( 'Fotos License', 'Fotos License', 'manage_options', 'fotos-license', array($this,'license_page' ));
}
function license_page() {
$license = get_option( 'ba_fotos_license_key' );
$status = get_option( 'ba_fotos_license_key_status' );
?>
<div class="wrap">
<h2><?php _e('Fotos License','fotos'); ?></h2>
<form method="post" action="options.php">
<?php settings_fields('ba_fotos_theme_license'); ?>
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row" valign="top">
<?php _e('License Key'); ?>
</th>
<td>
<input id="ba_fotos_license_key" name="ba_fotos_license_key" type="text" class="regular-text" value="<?php esc_attr( $license ); ?>" />
<label class="description" for="ba_fotos_license_key"><?php _e('Enter your license key to enable automatic updates.','fotos'); ?></label>
</td>
</tr>
<?php if( false !== $license ) { ?>
<tr valign="top">
<th scope="row" valign="top">
<?php _e('Activate License'); ?>
</th>
<td>
<?php if( $status !== false && $status == 'valid' ) { ?>
<span style="color:green;"><?php _e('active'); ?></span>
<?php wp_nonce_field( 'ba_fotos_nonce', 'ba_fotos_nonce' ); ?>
<input type="submit" class="button-secondary" name="ba_fotos_license_deactivate" value="<?php _e('Deactivate License','fotos'); ?>"/>
<?php } else {
wp_nonce_field( 'ba_fotos_nonce', 'ba_fotos_nonce' ); ?>
<input type="submit" class="button-secondary" name="ba_fotos_license_activate" value="<?php _e('Activate License','fotos'); ?>"/>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php submit_button(); ?>
</form>
<?php
}
function register_option() {
// creates our settings in the options table
register_setting('ba_fotos_theme_license', 'ba_fotos_license_key', array($this,'sanitize_license' ));
}
function sanitize_license( $new ) {
$old = get_option( 'ba_fotos_license_key' );
if( $old && $old != $new ) {
delete_option( 'ba_fotos_license_key_status' ); // new license has been entered, so must reactivate
}
return $new;
}
function activate_license() {
if( isset( $_POST['ba_fotos_license_activate'] ) ) {
if( ! check_admin_referer( 'ba_fotos_nonce', 'ba_fotos_nonce' ) )
return; // get out if we didn't click the Activate button
global $wp_version;
$license = trim( get_option( 'ba_fotos_license_key' ) );
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode( BA_FOTOS_THEME_NAME )
);
$response = wp_remote_get( add_query_arg( $api_params, BA_FOTOS_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );
if ( is_wp_error( $response ) )
return false;
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
// $license_data->license will be either "active" or "inactive"
update_option( 'ba_fotos_license_key_status', $license_data->license );
}
}
function deactivate_license() {
// listen for our activate button to be clicked
if( isset( $_POST['ba_fotos_license_deactivate'] ) ) {
// run a quick security check
if( ! check_admin_referer( 'ba_fotos_nonce', 'ba_fotos_nonce' ) )
return; // get out if we didn't click the Activate button
// retrieve the license from the database
$license = trim( get_option( 'ba_fotos_license_key' ) );
// data to send in our API request
$api_params = array(
'edd_action'=> 'deactivate_license',
'license' => $license,
'item_name' => urlencode( BA_FOTOS_THEME_NAME ) // the name of our product in EDD
);
// Call the custom API.
$response = wp_remote_get( add_query_arg( $api_params, BA_FOTOS_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );
// make sure the response came back okay
if ( is_wp_error( $response ) )
return false;
// decode the license data
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
// $license_data->license will be either "deactivated" or "failed"
if( $license_data->license == 'deactivated' )
delete_option( 'ba_fotos_license_key_status' );
}
}
function edd_sample_theme_check_license() {
global $wp_version;
$license = trim( get_option( 'ba_fotos_license_key' ) );
$api_params = array(
'edd_action' => 'check_license',
'license' => $license,
'item_name' => urlencode( BA_FOTOS_THEME_NAME )
);
$response = wp_remote_get( add_query_arg( $api_params, BA_FOTOS_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );
if ( is_wp_error( $response ) )
return false;
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if( $license_data->license == 'valid' ) {
echo 'valid'; exit;
// this license is still valid
} else {
echo 'invalid'; exit;
// this license is no longer valid
}
}
}
new baFotosTheme;