/
functions.inc.php
282 lines (210 loc) · 8.81 KB
/
functions.inc.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
<?php
function fetch_pr(){
global $wpdb;
global $prtools_debug;
global $prtools_url_table;
global $prtools_pr_table;
$sql="SELECT ID FROM " . $wpdb->posts . " WHERE post_status='publish'";
$published_posts = $wpdb->get_results($sql);
$prtools_settings=get_option('pagerank_tools_settings');
$lastcheck=time()-$prtools_settings['fetch_interval']*60;
$lastcheck_url=time()-$prtools_settings['fetch_url_interval']*24*60*60;
$lastcheck_url_new=time()-$prtools_settings['fetch_url_interval_new']*24*60*60;
$sql="SELECT * FROM ".$prtools_url_table." ORDER BY lastcheck DESC LIMIT 0,1";
$prtools_last = $wpdb->get_row($sql);
$sql="SELECT * FROM ".$prtools_url_table." WHERE queue=1 LIMIT 0,1";
$queued_urls = $wpdb->get_results($sql);
/*
echo "LU: ".$prtools_last->lastupdate."<br>";
$s=time()-$prtools_last->lastupdate;
echo "S: ".$s."<br>";
echo "T: ".time()."<br>";
*/
/*
* Checking time of last pr request
***************************************/
if($prtools_last->lastcheck<$lastcheck || $queued_urls>0){
// Getting sites which are updatable by settings
$sql="SELECT * FROM ".$prtools_url_table." WHERE (lastcheck<".$lastcheck_url." OR (lastcheck<".$lastcheck_url_new." AND pr=-1) OR (lastcheck<".$lastcheck_url_new." AND pr=-2)) OR queue=1 ORDER BY pr DESC LIMIT 0,".$prtools_settings['fetch_num'];
// $sql="SELECT * FROM ".$prtools_url_table." WHERE lastcheck<=".$lastcheck_url." ORDER BY pr DESC LIMIT 0,".$prtools_settings['fetch_num'];
$url_rows=$wpdb->get_results($sql);
if($prtools_debug) echo $sql."<br />\n";
if($prtools_debug) echo "Lastcheck to be maximum at: " . date("d.m.Y - H:i",$lastcheck)."<br />";
if($prtools_debug) echo "Lastcheck URL have to be maximum at: " . date("d.m.Y - H:i",$lastcheck_url)."<br />";
if($prtools_debug) echo "Lastcheck of new URL have to be maximum at: " . date("d.m.Y - H:i",$lastcheck_url)."<br />";
if($prtools_debug) print_r_html($prtools_settings);
if($prtools_debug) print_r_html($url_rows);
// Get all published urls
$published_blog_urls = get_blog_urls();
/*
* Checking pageranks
***************************************/
foreach($url_rows AS $url_row){
// If post is an actual published url
if( in_array( $url_row->url, $published_blog_urls) ){
if($prtools_debug){
$time_start=time();
}
if($prtools_debug) echo $lastcheck . " > " . $prtools_settings['last_google_request']."<br />";
if($lastcheck>$prtools_settings['last_google_request']){
$pr=getpagerank($url_row->url);
if($url_row->queue==1){
if($prtools_debug) echo "GETTING PR FOR QUEUE<br>URL: ".$url_row->url."<br />\n";
}else{
if($prtools_debug) echo "GETTING PR<br>URL: ".$url_row->url."<br />\n";
}
if($prtools_debug){
$time_end=time();
echo "Request time: ".($time_end-$time_start)."<br />\n";
}
if($pr==""){$pr=-1;}
// If PR is a new rank update and insert
$actual_pr=(int) $pr;
$last_pr=(int) $url_row->pr;
if($prtools_debug) echo "URL: ".$url_row->url."<br />";
if($prtools_debug) echo "New PR: ".$actual_pr." <br />Old PR: ".$last_pr."<br />";
if($actual_pr!=$last_pr){
if($prtools_debug) echo "<b>updating</b><br /><br />";
$diff_last_pr = $actual_pr - $last_pr;
$url_row->pr_entries++;
$wpdb->update($prtools_url_table, array('lastcheck'=>time(),'lastupdate'=>time(),'pr'=>$actual_pr,'diff_last_pr'=>$diff_last_pr, 'pr_entries'=>$url_row->pr_entries, 'queue' => 0), array('url'=>$url_row->url));
$wpdb->insert($prtools_pr_table,array('entrydate'=>time(),'url'=>$url_row->url,'pr'=>$actual_pr));
do_action( 'prtools_update_pr',$url_row,$actual_pr,$last_pr);
}else{
if($prtools_debug) echo "<b>not updating</b><br /><br />";
$wpdb->update($prtools_url_table, array('lastcheck'=>time(), 'queue' => 0), array('url'=>$url_row->url));
}
}
}
}
$prtools_settings['last_google_request']=time();
update_option('pagerank_tools_settings',$prtools_settings);
}
}
function fetch_pr_sidewide(){
global $wpdb;
global $prtools_debug;
global $prtools_url_table;
global $prtools_pr_table;
// Getting sites which are updatable by settings
$sql="SELECT * FROM ".$prtools_url_table;
$url_rows=$wpdb->get_results($sql);
/*
* Checking pageranks
***************************************/
foreach($url_rows AS $url_row){
if($prtools_debug){
$time_start=time();
echo "GETTING PR<br>URL: ".$url_row->url."<br />\n";
}
$pr=getpagerank($url_row->url);
if($prtools_debug){
$time_end=time();
echo "Request time: ".($time_end-$time_start)."<br />\n";
}
if($pr==""){$pr=-1;}
// echo "PR: ".$pr." (".$prtools_row->url.")<br>";
$wpdb->update($prtools_url_table, array('lastupdate'=>time(),'pr'=>$pr), array('url'=>$url_row->url));
// If PR is a new rank update and insert
if($pr!=$url_row->pr){
$diff_last_pr = $pr - $url_row->pr;
$url_row->pr_entries++;
$wpdb->update($prtools_url_table, array('lastupdate'=>time(),'pr'=>$pr,'diff_last_pr'=>$diff_last_pr, 'pr_entries'=>$url_row->pr_entries), array('url'=>$url_row->url));
$wpdb->insert($prtools_pr_table,array('entrydate'=>time(),'url'=>$url_row->url,'pr'=>$pr));
}
}
}
function get_blog_urls(){
$urls = array();
$urls= array_merge( $urls, wp_get_post_urls() );
$urls= array_merge( $urls, wp_get_page_urls() );
$urls= array_merge( $urls, wp_get_cat_urls() );
$urls= array_merge( $urls, wp_get_tag_urls() );
$urls[]=get_bloginfo("url").'/';
return $urls;
}
function update_url_table($update_posts=true,$update_pages=true){
global $wpdb;
global $prtools_url_table;
global $prtools_pr_table;
if($update_posts){
$urls=wp_get_post_urls();
foreach($urls AS $url){pr_insert_url($url);}
}
if($update_pages){
$urls=wp_get_page_urls();
foreach($urls AS $url){pr_insert_url($url);}
}
$urls=wp_get_cat_urls();
foreach($urls AS $url){pr_insert_url($url);}
$urls=wp_get_tag_urls();
foreach($urls AS $url){pr_insert_url($url);}
$home=get_bloginfo("url").'/';
pr_insert_url($home);
}
function pr_insert_url_by_post_ID( $post_ID ){
$post=get_post( $post_ID );
if( $post->post_status == "publish" ){
$url = get_permalink( $post_ID );
pr_insert_url( $url );
}
}
add_action('save_post' , 'pr_insert_url_by_post_ID', 1, 1);
function pr_insert_url($url){
global $wpdb;
global $prtools_url_table;
global $prtools_pr_table;
$prtools_rows = $wpdb->get_results( "SELECT * FROM ".$prtools_url_table." WHERE url='".$url."'");
if(count($prtools_rows)==0){
$wpdb->insert($prtools_url_table,array('entrydate'=>time(),'lastupdate'=>time(),'url_type'=>'post','url'=>$url,'pr'=>-2));
$wpdb->insert($prtools_pr_table,array('entrydate'=>time(),'url'=>$url,'pr'=>-2));
}
}
function pr_save_settings(){
if(isset($_POST['savesettings'])){
if($_POST['savesettings']!=""){
$prtools_settings=get_option('pagerank_tools_settings');
$prtools_settings['fetch_interval']=$_POST['fetch_interval'];
$prtools_settings['fetch_url_interval']=$_POST['fetch_url_interval'];
$prtools_settings['fetch_url_interval_new']=$_POST['fetch_url_interval_new'];
$prtools_settings['fetch_num']=$_POST['fetch_num'];
update_option('pagerank_tools_settings',$prtools_settings);
do_action( 'prtools_settings_save');
}
}
}
add_action( 'admin_init', 'pr_save_settings' , 10);
function pr_delete_url(){
global $wpdb;
global $prtools_url_table;
global $prtools_pr_table;
if(isset($_GET['delete_url'])){
if($_GET['delete_url']!=""){
$sql = 'DELETE FROM ' . $prtools_url_table . ' WHERE url="' . $_GET['delete_url'] . '"';
$wpdb->query($sql);
$sql = 'DELETE FROM ' . $prtools_pr_table . ' WHERE url="' . $_GET['delete_url'] . '"';
$wpdb->query($sql);
}
}
}
add_action( 'admin_init', 'pr_delete_url' , 10);
if(!$prtools_extended){
function pr_get_pro_stats(){
global $prtools_plugin_path;
echo '<div class="statistics">
<div class="diagram" style="margin:0.83em 0;">
<a class="get_pro" href="#" title="Get professional version of pagerank tools"><img src="' . $prtools_plugin_path . '/images/statistics_get_pro.jpg" alt="Pagerank overview" border="0"></a>
</div>
</div>';
}
add_action( 'prtools_main_head', 'pr_get_pro_stats' , 10, 0);
function pr_get_pro_footer(){
echo '<a class="get_pro" href="#" title="Get professional version of pagerank tools"><img src="' . $prtools_plugin_path . '/images/footer_get_pro.jpg" alt="Pagerank overview" border="0"></a>';
}
add_action( 'prtools_main_bottom', 'pr_get_pro_footer' , 10, 0);
function pr_get_pro_email(){
echo '<br /><br /><a class="get_pro" href="#" title="Get professional version of pagerank tools"><img src="' . $prtools_plugin_path . '/images/email_get_pro.jpg" /></a>';
}
add_action( 'prtools_settings_page', 'pr_get_pro_email' , 10, 0);
}
?>