-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathwpwsl_core.php
executable file
·193 lines (165 loc) · 5.35 KB
/
wpwsl_core.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
<?php
/**
* Plugin Name: WeChat Subscribers Lite
* Plugin URI: http://www.imredy.com/wp-wechat/
* Description: 轻便易用的微信(weixin)公众平台订阅号管理工具。Light weight WeChat (Subscribers) public platform management tool.
* Version: 1.7.0
* Author: Redy Ru, Robin Zhao
* Author URI: http://www.imredy.com/
* License: GPLv2 or later
* Text Domain: WPWSL
* Domain Path: /languages
*/
if (!defined('ABSPATH')) {
exit;
}
define('WPWSL_PLUGIN_URL', plugins_url('', __FILE__));
define('WPWSL_GENERAL_PAGE', 'wpwsl-general-page');
define('WPWSL_HISTORY_PAGE', 'wpwsl-history-page');
define('WPWSL_SETTINGS_PAGE', 'wpwsl-settings-page');
define('WPWSL_SETTINGS_OPTION', 'wpwsl_settings_option');
define('SELECT_ROWS_AMOUNT', 100);
define('SYNC_TITLE_LIMIT', 50);
define('SYNC_CONTENT_LIMIT', 300);
define('SYNC_EXCERPT_LIMIT', 100);
define('MAX_SEARCH_LIMIT', 6);
define('DB_TABLE_WPWSL_HISTORY', 'wechat_subscribers_lite_history');
//Interface
$options = get_option(WPWSL_SETTINGS_OPTION);
global $token;
$token = isset($options['token']) ? $options['token'] : '';
add_action('parse_request', 'load_interface');
//Languages
add_action('plugins_loaded', 'load_languages_file');
//create db table wechat_subscribers_lite_messages
add_action('plugins_loaded', 'create_history_table');
add_action('after_setup_theme', 'set_wechat_img_size');
add_filter('image_size_names_choose', 'sup_wechat_custom_sizes');
//Setup Admin
add_action('_admin_menu', 'wpwsl_admin_setup');
//AJAX handle
add_action('admin_init', 'ajax_handle', 999);
//Safe Redirect
add_action('admin_init', 'safe_redirect', 999);
//Scripts
add_action('admin_print_scripts', 'custom_admin_scripts');
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'wpwsl_plugin_settings_link');
//utils
function trim_words($str, $limit, $suffix = '...', $db_charset = DB_CHARSET,
$strip_tags = true)
{
if ($strip_tags) {
$str = strip_tags($str);
}
if (strpos($db_charset, "utf8") !== false) {
$db_charset = "utf8";
}
$new_str = mb_substr($str, 0, $limit, $db_charset);
$new_str = mb_strlen($str, $db_charset) > $limit ? $new_str . $suffix : $new_str;
return $new_str;
}
function load_interface()
{
global $token;
if ($token != '' && isset($_GET[$token])) {
require( 'interface.php' );
}
}
function load_languages_file()
{
load_plugin_textdomain('WPWSL', false, dirname(plugin_basename(__FILE__)) . '/languages/');
}
function create_history_table()
{
global $wpdb;
$table_name = DB_TABLE_WPWSL_HISTORY;
$sql = "CREATE TABLE $table_name (
id bigint(20) NOT NULL KEY AUTO_INCREMENT,
openid varchar(100) NOT NULL,
keyword varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
is_match char(1) NOT NULL,
time datetime NOT NULL
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
//Setup wechat image size
function set_wechat_img_size()
{
add_image_size('sup_wechat_big', 360, 200, true);
add_image_size('sup_wechat_small', 200, 200, true);
}
function sup_wechat_custom_sizes($sizes)
{
return array_merge($sizes, [
'sup_wechat_big' => __('WeChat big image', 'WPWSL'),
'sup_wechat_small' => __('WeChat small image', 'WPWSL')
]);
}
//Setup Admin
function wpwsl_admin_setup(){
global $user_level;
if($user_level>=5){
require_once( 'posttype_wpwsl_template.php' );
$page_title=__('WeChat Subscribers Lite', 'WPWSL');
$menu_title=__('WeChat Subscribers Lite', 'WPWSL');
$capability='edit_pages';
$menu_slug=WPWSL_GENERAL_PAGE;
$function='';
$icon_url=WPWSL_PLUGIN_URL.'/img/wpwsl_icon_16.png';
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url );
require_once( 'class-wpwsl-settings.php' );
require_once( 'class-wpwsl-general.php' );
require_once( 'class-wpwsl-history.php' );
//Settings
$settingObject=WPWSL_Settings::get_instance();
//General
$generalObject=WPWSL_General::get_instance();
//History
$hitsotryObject=WPWSL_History::get_instance();
}
}
function ajax_handle()
{
require_once( 'ajax_request_handle.php');
}
function safe_redirect()
{
if (isset($_GET['_wp_http_referer'])) {
wp_redirect(remove_query_arg([
'_wp_http_referer',
'_wpnonce'], stripslashes($_SERVER['REQUEST_URI'])));
}
}
//add custom upload jquery support.
function custom_admin_scripts()
{
wp_enqueue_script('jquery');
wp_enqueue_media();
wp_register_script('custom-upload', WPWSL_PLUGIN_URL . '/js/custom_upload.js', [
'jquery',
'media-upload',
'thickbox'], "2.0");
wp_enqueue_script('custom-upload');
wp_register_script('modal', WPWSL_PLUGIN_URL . '/js/modal.js', [], "2.0");
wp_enqueue_script('modal');
}
// Add settings link on plugin page
function wpwsl_plugin_settings_link($links)
{
$settings_link = '<a href="' . menu_page_url(WPWSL_SETTINGS_PAGE, false) . '">' . __('Settings', 'WPWSL') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
class WpwslHelper
{
public static function get($name, $default = '')
{
return isset($_GET[$name]) ? $_GET[$name] : $default;
}
public static function post($name, $default = '')
{
return isset($_POST[$name]) ? $_POST[$name] : $default;
}
}