-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjust-update-services-merge.php
179 lines (155 loc) · 4.93 KB
/
just-update-services-merge.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
<?php
/*
Plugin Name: Just Update Services Merge
Plugin URI:
Description: Provide simple interface to edit "Update Services" list.
Tags: update services, ping sites, seo
Author: Alexander Prokopenko
Author URI: http://justcoded.com/
Version: 1.0
Donate link:
*/
define('JUSM_ROOT', dirname(__FILE__));
define('', 'just-update-services');
if(!function_exists('pa')){
function pa($mixed, $stop = false) {
$ar = debug_backtrace(); $key = pathinfo($ar[0]['file']); $key = $key['basename'].':'.$ar[0]['line'];
$print = array($key => $mixed); echo( '<pre>'.htmlentities(print_r($print,1)).'</pre>' );
if($stop == 1) exit();
}
}
/**
* Plugin init
*/
add_action('plugins_loaded', 'jusm_init');
function jusm_init(){
if( !is_admin() ) return;
/**
* load translations
*/
load_plugin_textdomain( JUS_TEXTDOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// Add admin page
add_action( 'admin_menu', 'jusm_admin_menu' );
// AJAX
add_action('wp_ajax_jusm_ajax_remove_url', 'jusm_ajax_remove_url');
add_action('wp_ajax_jusm_ajax_ping_site', 'jusm_ajax_ping_site');
}
/**
* Init admin menu for just update services page
*/
function jusm_admin_menu(){
add_options_page( __('Just Update Services'), __('Just Update Services'), 'manage_options', 'jusm_update_services', 'jusm_admin_settings_page');
}
/**
* Show plugin settings page
*/
function jusm_admin_settings_page(){
// Form submit processing
if( !empty($_POST['jusm_submit']) && (!empty($_POST['jusm_add_ping_sites']) || !empty($_POST['jusm_del_ping_sites'])) ) {
jusm_update_ping_sites();
}
// Get list of ping sites
$jusm_ping_sites = jusm_get_ping_sites();
// Load template
include( JUSM_ROOT . '/templates/settings_page.tpl.php' );
}
/**
* Properly enqueue styles and scripts for our settings page.
*/
add_action( 'admin_print_styles', 'jusm_admin_styles' );
function jusm_admin_styles( $hook_suffix ) {
wp_enqueue_style( 'jusm_update_services', plugins_url( 'assets/styles.css' , __FILE__ ) );
}
add_action( 'admin_print_scripts', 'jusm_admin_scripts' );
function jusm_admin_scripts( $hook_suffix ) {
wp_enqueue_script( 'jusm_update_services', plugins_url( 'assets/settings_page.js' , __FILE__ ) );
// add text domain
wp_localize_script( 'jusm_update_services', 'text_jusm', jusm_get_language_strings() );
}
/**
* translation strings for javascript
*/
function jusm_get_language_strings(){
$strings = array(
'err_validate_delete_urls' => __('Please add URLs to delete first.'),
'confirm_delete_single' => __('Are you sure you want to delete "!ping_site"?'),
'err_ajax_delete' => __('Unable to delete "!ping_site". Please try again'),
'confirm_delete_multiple' => __('Are you sure you want to delete all URLs listed above?'),
'err_ajax_ping' => __('Unable to ping site. Please try again later.'),
);
return $strings;
}
/**
* Get ping sites
*/
function jusm_get_ping_sites() {
$str = get_option('ping_sites');
$result = FALSE;
if( !empty($str) ) {
$result = explode('___', preg_replace("/[\s\t\n\r]+/", '___', $str));
sort($result, SORT_STRING);
}
return $result;
}
/**
* Check url
*/
function jusm_check_url($urls = FALSE) {
$result = FALSE;
if( $urls ) {
foreach($urls as $url) {
if(preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url)) $result[] = $url;
}
}
return $result;
}
/**
* Update ping sites
*/
function jusm_update_ping_sites() {
$value = $_POST['jusm_add_ping_sites'] ? explode('___', preg_replace("/[\s\t\n\r]+/", '___', $_POST['jusm_add_ping_sites'])) : explode('___', preg_replace("/[\s\t\n\r]+/", '___', $_POST['jusm_del_ping_sites']));
$value = jusm_check_url($value);
if( $value ) {
$ping_sites = jusm_get_ping_sites();
if( !empty($_POST['jusm_add_ping_sites']) ) {
$result = array_unique(array_merge($ping_sites, $value));
}
else {
$result = array_unique(array_diff($ping_sites, $value));
}
sort($result, SORT_STRING);
$result = implode("\n\n", $result);
update_option('ping_sites', $result);
}
}
/**
* Remove url AJAX
*/
function jusm_ajax_remove_url() {
if ( !empty($_POST['jusm_del_ping_sites']) ) {
jusm_update_ping_sites();
}
die();
}
/**
* Ping site AJAX
*/
function jusm_ajax_ping_site() {
if ( !empty($_POST['jusm_ping_site']) ) {
$url = $_POST['jusm_ping_site'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt ($ch, CURLOPT_REFERER, "http://google.com");
curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
// return response ans stop script
echo $status_code;
exit;
}
?>