-
Notifications
You must be signed in to change notification settings - Fork 16
/
woocommerce-dadata.php
174 lines (151 loc) · 5.72 KB
/
woocommerce-dadata.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: WooCommerce DaData <3
* Description: Поддержка подсказок dadata.ru - быстрый ввод адресов, банков, организаций, ФИО и email.
* Plugin URI: http://github.com/troyanov/woocommerce-dadata
* Version: 1.2.0
*
* @package WooCommerce_DaData
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Main WooCommerce_DaData Class
*
* @class WooCommerce_DaData
* @version 1.1.0
* @package WooCommerce_DaData
*/
final class WooCommerce_DaData {
/**
* Set up the plugin
*/
public function __construct() {
add_action( 'init', array( $this, 'WooCommerce_DaData_setup' ), -1 );
require_once( 'custom/functions.php' );
}
/**
* Setup all the things
*/
public function WooCommerce_DaData_setup() {
add_action( 'wp_enqueue_scripts', array( $this, 'WooCommerce_DaData_css' ), 999 );
add_action( 'wp_enqueue_scripts', array( $this, 'WooCommerce_DaData_js' ));
add_filter( 'template_include', array( $this, 'WooCommerce_DaData_template' ), 11 );
add_filter( 'wc_get_template', array( $this, 'WooCommerce_DaData_wc_get_template' ), 11, 5 );
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab'), 50 );
add_action( 'woocommerce_settings_tabs_settings_tab_wcddc', array( $this, 'settings_tab') );
add_action( 'woocommerce_update_options_settings_tab_wcddc', array( $this, 'update_settings') );
}
/**
* Enqueue the CSS
*
* @return void
*/
public function WooCommerce_DaData_css() {
wp_enqueue_style( 'custom-css', plugins_url( '/custom/style.css', __FILE__ ) );
}
/**
* Enqueue the Javascript
*
* @return void
*/
public function WooCommerce_DaData_js() {
wp_enqueue_script( 'custom-js', plugins_url( '/custom/custom.js', __FILE__ ), array( 'jquery' ) );
wp_register_script(
'jquery.suggestions.min',
plugins_url( '/custom/assets/js/jquery.suggestions.min.js', __FILE__ ),
array( 'jquery' )
);
wp_enqueue_script( 'jquery.suggestions.min' );
wp_register_script(
'jquery.xdomainrequest.min',
plugins_url( '/custom/assets/js/jquery.xdomainrequest.min.js', __FILE__),
array( 'jquery' )
);
wp_enqueue_script( 'jquery.suggestions.min' );
wp_enqueue_style( 'suggestions',
plugins_url( '/custom/assets/css/suggestions.css', __FILE__) );
$dataToBePassed = array(
'dadata_suggest_token' => get_option('wc_settings_tab_wcddc_dadata_suggest_token')
);
wp_localize_script( 'custom-js', 'php_vars', $dataToBePassed );
}
/**
* Look in this plugin for template files first.
* This works for the top level templates (IE single.php, page.php etc). However, it doesn't work for
* template parts yet (content.php, header.php etc).
*
* Relevant trac ticket; https://core.trac.wordpress.org/ticket/13239
*
* @param string $template template string.
* @return string $template new template string.
*/
public function WooCommerce_DaData_template( $template ) {
if ( file_exists( untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/custom/templates/' . basename( $template ) ) ) {
$template = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/custom/templates/' . basename( $template );
}
return $template;
}
/**
* Look in this plugin for WooCommerce template overrides.
*
* For example, if you want to override woocommerce/templates/cart/cart.php, you
* can place the modified template in <plugindir>/custom/templates/woocommerce/cart/cart.php
*
* @param string $located is the currently located template, if any was found so far.
* @param string $template_name is the name of the template (ex: cart/cart.php).
* @return string $located is the newly located template if one was found, otherwise
* it is the previously found template.
*/
public function WooCommerce_DaData_wc_get_template( $located, $template_name, $args, $template_path, $default_path ) {
$plugin_template_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/custom/templates/woocommerce/' . $template_name;
if ( file_exists( $plugin_template_path ) ) {
$located = $plugin_template_path;
}
return $located;
}
public function add_settings_tab( $settings_tabs ) {
$settings_tabs['settings_tab_wcddc'] = __( 'DaData', 'woocommerce-settings-tab-wcddc' );
return $settings_tabs;
}
public function settings_tab() {
woocommerce_admin_fields( $this->get_settings() );
}
public function update_settings() {
woocommerce_update_options( $this->get_settings() );
}
public function get_settings() {
$wcddc_settings = array(
'section_title' => array(
'name' => __( 'DaData Подсказки', 'woocommerce-settings-tab-wcddc' ),
'type' => 'title',
'desc' => '',
'id' => 'wc_settings_tab_wcddc_section_title'
),
'wc_settings_tab_wcddc_dadata_suggest_token' => array(
'name' => __( 'API-ключ', 'woocommerce-settings-tab-wcddc' ),
'type' => 'text',
'desc' => __( '<a href="https://dadata.ru/api/suggest/#registration_popup">Где взять ключ?</a>', 'woocommerce-settings-tab-wcddc' ),
'id' => 'wc_settings_tab_wcddc_dadata_suggest_token'
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_settings_tab_wcddc_section_end'
)
);
return apply_filters( 'wc_settings_tab_wcddc_settings', $wcddc_settings );
}
} // End Class
/**
* The 'main' function
*
* @return void
*/
function WooCommerce_DaData_main() {
new WooCommerce_DaData();
}
/**
* Initialise the plugin
*/
add_action( 'plugins_loaded', 'WooCommerce_DaData_main' );