-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-zipcheck.php
121 lines (106 loc) · 4 KB
/
wordpress-zipcheck.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
<?php
/**
* Plugin Name: Nextpertise Wordpress Zipcheck
* Description: Plug-in to implement the zipcode checker from Nextpertise
* Version: 1.0.0
* Author: Len van Essen
* Author URI: http://wndr.digital
*
* @package wordpress-zipcheck
*/
/**
* Wordpress Zipcheck Class.
*/
class WordpessZipcheck {
/**
* Plugin path.
*
* @var string
*/
private $plugin_path;
/**
* WordPress Settings Framework instance.
*
* @var WordPressSettingsFramework
*/
private $wpsf;
/**
* WPSFTest constructor.
*/
public function __construct() {
$this->plugin_path = plugin_dir_path( __FILE__ );
// Include and create a new WordPressSettingsFramework.
require_once $this->plugin_path . 'wp-settings-framework.php';
$this->wpsf = new WordPressSettingsFramework( $this->plugin_path . 'settings/wordpress-zipcheck.php', 'nextpertise' );
// Add admin menu.
add_action( 'admin_menu', array( $this, 'add_settings_page' ), 20 );
// Add an optional settings validation filter (recommended).
add_filter( $this->wpsf->get_option_group() . '_settings_validate', array( &$this, 'validate_settings' ) );
// Define CDN url
defined( 'ZIPCHECK_JS_URI' ) || define( 'ZIPCHECK_JS_URI', 'https://cdn.jsdelivr.net/gh/Nextpertise/js-zipcode-check-plugin/dist/js/app.js' );
// Enqueue JS
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_script(
'nextperise_zipcheck_js',
ZIPCHECK_JS_URI
, [], '1.0.0');
} );
$this->add_shortcode();
}
/**
* Add settings page.
*/
public function add_settings_page() {
$this->wpsf->add_settings_page(
array(
'parent' => 'general',
'page_title' => esc_html__( 'Zipcheck Settings', 'wordpress-zipcheck' ),
'menu_title' => esc_html__( 'Zipcheck Settings', 'wordpress-zipcheck' ),
'capability' => 'edit_pages',
)
);
}
/**
* Validate settings.
*
* @param mixed $input Input data.
*
* @return mixed $input
*/
public function validate_settings( $input ) {
// Do your settings validation here
// Same as $sanitize_callback from http://codex.wordpress.org/Function_Reference/register_setting.
return $input;
}
/**
* Registers the shortcode
* @return void
*/
public function add_shortcode() {
add_shortcode( 'nextpertise_zipcheck', function($args) {
$token = wpsf_get_setting('nextpertise', 'general', 'token');
if(empty($token)) {
echo '<h3>Error, please provide your API token under Zipcheck Settings in WordPress';
return;
}
$primary_color = wpsf_get_setting('nextpertise', 'layout', 'primary_color');
$secondary_color = wpsf_get_setting('nextpertise', 'layout', 'secondary_color');
$background_color = wpsf_get_setting('nextpertise', 'layout', 'background_color');
$input_border_top_color = wpsf_get_setting('nextpertise', 'layout', 'input_border_top_color');
$input_background_color = wpsf_get_setting('nextpertise', 'layout', 'input_background_color');
$button_effect = wpsf_get_setting('nextpertise', 'layout', 'button_effect');
$redirect = isset($args['redirect']) ? $args['redirect'] : null;
return "<nextpertise-zipcheck
primary-color='$primary_color'
secondary-color='$secondary_color'
background-color='$background_color'
token='$token'
input-border-top-color='$input_border_top_color'
input-background-color='$input_background_color'
". ($button_effect ? "button-effect" : '')."
redirect='$redirect'
></nextpertise-zipcheck>";
});
}
}
$wpsf_test = new WordpessZipcheck();