-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexing-api.php
123 lines (109 loc) · 3.99 KB
/
indexing-api.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
<?php
// @codingStandardsIgnoreLine
/*
* Plugin Name: Mageinn Indexing Api
* Description: Indexing Api
* Version: 1.0.0
* Author: Mageinn
* Author URI: https://www.mageinn.com
* Copyright: 2021 Mageinn LLC
*/
if (!defined('ABSPATH')) {
exit; // disable direct access
}
/*
* Compana classes
*/
require_once(ABSPATH . 'wp-content/plugins/compana-elements/compana-api/CompanaOptions.php');
require_once(ABSPATH . 'wp-content/plugins/compana-elements/compana-api/options.php');
require_once(ABSPATH . 'wp-content/plugins/compana-elements/compana-api/CompanaApi.php');
require_once(ABSPATH . 'wp-content/plugins/compana-elements/compana-api/LinkedinConnector.php');
// For DB
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// For IndexingApi
require_once (ABSPATH . 'wp-content/plugins/indexing-api/vendor/autoload.php');
/**
* @throws \Google\Exception
*/
function updateGoogleIndex(){
$client = new Google\Client();
$client->setAuthConfig(ABSPATH .'wp-content/plugins/indexing-api/credentials.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://www.indexing.googleapis.com/v3/urlNotifications:publish';
/**
* Cheking for table and if it isn't create a new one
*/
global $wpdb;
$table_name = $wpdb->get_blog_prefix() . 'compana_jobs';
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset} COLLATE {$wpdb->collate}";
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
if ( ! $wpdb->get_var( $query ) == $table_name ) {
$sql = "CREATE TABLE {$table_name} (
id int(11) unsigned NOT NULL auto_increment,
url varchar(255) NOT NULL default '',
PRIMARY KEY (id)
) {$charset_collate};";
dbDelta($sql);
}
/*
* Get urls from compana and put them into database and send them to Indexing
*/
$jobList = CompanaApi::GetJobs(array(), false, "pm");
$site_url = "https://yoursite.com";
$sql= "";
$db_urls = $wpdb->get_results("SELECT url FROM $table_name");
foreach ($jobList as $jobItem) {
$url= $site_url . $jobItem['alias'];
$exist = false;
foreach ($db_urls as $db_url){
if($url == $db_url->url){
$exist = true;
}
}
if($exist == false) {
$sql .= "INSERT INTO $table_name (url) VALUE ('$url') ;";
$content = '{
"url": " ' . $url . ' ",
"type": "URL_UPDATED"
}';
$httpClient->post($endpoint, [ 'body' => $content ]);
}
}
/**
* Get urls from database and, if it not exist in compana, delete them from dn and IndexingApi
*/
foreach ($db_urls as $db_url) {
$url = $db_url->url;
$exist = false;
foreach ($jobList as $jobItem){
if($url == $site_url . $jobItem['alias']){
$exist = true;
}
}
if($exist == false) {
$wpdb->delete( $table_name, array( 'url' => $url ) );
$content = '{
"url": " ' . $url . ' ",
"type": "URL_DELETED"
}';
$httpClient->post($endpoint, [ 'body' => $content ]);
}
}
dbDelta($sql);
}
register_activation_hook(__FILE__, 'google_index_activation');
function google_index_activation() {
wp_clear_scheduled_hook( 'update_urls_twicedaily' );
// добавим новую cron задачу
wp_schedule_event( time(), 'twicedaily', 'update_urls_twicedaily');
}
add_action( 'update_urls_twicedaily', 'update_compana_urls_twicedaily' );
function update_compana_urls_twicedaily() {
updateGoogleIndex();
}
register_deactivation_hook( __FILE__, 'google_index_deactivation' );
function google_index_deactivation(){
wp_clear_scheduled_hook( 'update_urls_twicedaily' );
}