|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Installs: Install Version 1.0.0. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 3.18.0 |
|
6
|
|
|
* @package Wordlift |
|
7
|
|
|
* @subpackage Wordlift/install |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Define the {@link Wordlift_Install_1_0_0} interface. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 3.18.0 |
|
14
|
|
|
* @package Wordlift |
|
15
|
|
|
* @subpackage Wordlift/install |
|
16
|
|
|
*/ |
|
17
|
|
|
class Wordlift_Install_1_0_0 extends Wordlift_Install { |
|
18
|
|
|
/** |
|
19
|
|
|
* @inheritdoc |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $version = '1.0.0'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The WordLift entity type terms. |
|
25
|
|
|
* |
|
26
|
|
|
* @since 3.18.0 |
|
27
|
|
|
* @access private |
|
28
|
|
|
* @var array $terms The entity type terms. |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $terms = array( |
|
31
|
|
|
'thing' => array( |
|
32
|
|
|
'label' => 'Thing', |
|
33
|
|
|
'description' => 'A generic thing (something that doesn\'t fit in the previous definitions.', |
|
34
|
|
|
), |
|
35
|
|
|
'creative-work' => array( |
|
36
|
|
|
'label' => 'CreativeWork', |
|
37
|
|
|
'description' => 'A creative work (or a Music Album).', |
|
38
|
|
|
), |
|
39
|
|
|
'event' => array( |
|
40
|
|
|
'label' => 'Event', |
|
41
|
|
|
'description' => 'An event.', |
|
42
|
|
|
), |
|
43
|
|
|
'organization' => array( |
|
44
|
|
|
'label' => 'Organization', |
|
45
|
|
|
'description' => 'An organization, including a government or a newspaper.', |
|
46
|
|
|
), |
|
47
|
|
|
'person' => array( |
|
48
|
|
|
'label' => 'Person', |
|
49
|
|
|
'description' => 'A person (or a music artist).', |
|
50
|
|
|
), |
|
51
|
|
|
'place' => array( |
|
52
|
|
|
'label' => 'Place', |
|
53
|
|
|
'description' => 'A place.', |
|
54
|
|
|
), |
|
55
|
|
|
'localbusiness' => array( |
|
56
|
|
|
'label' => 'LocalBusiness', |
|
57
|
|
|
'description' => 'A local business.', |
|
58
|
|
|
), |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function install() { |
|
65
|
|
|
// Srt the dataset uri. |
|
66
|
|
|
$this->set_dataset_uri(); |
|
67
|
|
|
|
|
68
|
|
|
// Create entity type terms. |
|
69
|
|
|
$this->create_entity_type_terms(); |
|
70
|
|
|
|
|
71
|
|
|
// Create relations table. |
|
72
|
|
|
$this->create_relation_instance_table(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Create required entity type terms |
|
77
|
|
|
* |
|
78
|
|
|
* @since 3.18.0 |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
private function create_entity_type_terms() { |
|
83
|
|
|
$this->log->debug( 'Installing Entity Type data...' ); |
|
84
|
|
|
|
|
85
|
|
|
// Set the taxonomy data. |
|
86
|
|
|
// Note: parent types must be defined before child types. |
|
87
|
|
|
foreach ( self::$terms as $slug => $term ) { |
|
88
|
|
|
|
|
89
|
|
|
// Check whether the term exists and create it if it doesn't. |
|
90
|
|
|
$term_id = $this->get_term_or_create_if_not_exists( $slug ); |
|
91
|
|
|
|
|
92
|
|
|
// Bail if the term doens't exists or it's not created. |
|
93
|
|
|
if ( empty( $term_id ) ) { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Update term with description, slug and parent. |
|
98
|
|
|
$term = wp_update_term( |
|
99
|
|
|
$term_id, |
|
100
|
|
|
Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
101
|
|
|
array( |
|
102
|
|
|
'name' => $term['label'], |
|
103
|
|
|
'slug' => $slug, |
|
104
|
|
|
'description' => $term['description'], |
|
105
|
|
|
// We give to WP taxonomy just one parent. TODO: see if can give more than one. |
|
106
|
|
|
'parent' => 0, |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$this->log->trace( "Entity Type $slug installed with ID {$term['term_id']}." ); |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->log->debug( 'Entity Type data installed.' ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Install custom relations table. |
|
119
|
|
|
* |
|
120
|
|
|
* @since 3.8.0 |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
private function create_relation_instance_table() { |
|
125
|
|
|
global $wpdb; |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$table_name = $wpdb->prefix . WL_DB_RELATION_INSTANCES_TABLE_NAME; |
|
128
|
|
|
$charset_collate = $wpdb->get_charset_collate(); |
|
129
|
|
|
|
|
130
|
|
|
// Sql statement for the relation instances custom table. |
|
131
|
|
|
$sql = <<<EOF |
|
132
|
|
|
CREATE TABLE $table_name ( |
|
133
|
|
|
id int(11) NOT NULL AUTO_INCREMENT, |
|
134
|
|
|
subject_id int(11) NOT NULL, |
|
135
|
|
|
predicate char(10) NOT NULL, |
|
136
|
|
|
object_id int(11) NOT NULL, |
|
137
|
|
|
UNIQUE KEY id (id), |
|
138
|
|
|
KEY subject_id_index (subject_id), |
|
139
|
|
|
KEY object_id_index (object_id) |
|
140
|
|
|
) $charset_collate; |
|
141
|
|
|
EOF; |
|
142
|
|
|
|
|
143
|
|
|
// @see: https://codex.wordpress.org/Creating_Tables_with_Plugins |
|
144
|
|
|
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
|
145
|
|
|
$results = dbDelta( $sql ); |
|
146
|
|
|
|
|
147
|
|
|
$this->log->info( $results ); |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Configure the dataset uri. |
|
153
|
|
|
* |
|
154
|
|
|
* @since 3.18.0 |
|
155
|
|
|
* |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
private function set_dataset_uri() { |
|
159
|
|
|
// Get the configuration service and load the key. |
|
160
|
|
|
$configuration_service = Wordlift_Configuration_Service::get_instance(); |
|
161
|
|
|
$key = $configuration_service->get_key(); |
|
162
|
|
|
|
|
163
|
|
|
// If the key is not empty then set the dataset URI while sending |
|
164
|
|
|
// the site URL. |
|
165
|
|
|
if ( ! empty( $key ) ) { |
|
166
|
|
|
$this->log->info( 'Updating the remote dataset URI...' ); |
|
167
|
|
|
|
|
168
|
|
|
$configuration_service->get_remote_dataset_uri( array( 'key' => $key, ) ); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Check if the dataset key has been stored. |
|
172
|
|
|
$dataset_uri = $configuration_service->get_dataset_uri(); |
|
173
|
|
|
|
|
174
|
|
|
// If the dataset URI is empty, do not set the install version. |
|
175
|
|
|
if ( empty( $dataset_uri ) ) { |
|
176
|
|
|
$this->log->info( 'Setting dataset URI filed: the dataset URI is empty.' ); |
|
177
|
|
|
|
|
178
|
|
|
return; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Checks whether the term exists and create it if it doesn't. |
|
184
|
|
|
* |
|
185
|
|
|
* @since 3.18.0 |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $slug Term slug. |
|
188
|
|
|
* |
|
189
|
|
|
* @return mixed Term id if the term exists or if it's created. False on failure |
|
190
|
|
|
*/ |
|
191
|
|
|
private function get_term_or_create_if_not_exists( $slug ) { |
|
192
|
|
|
// Create the term if it does not exist, then get its ID. |
|
193
|
|
|
$term_id = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
194
|
|
|
|
|
195
|
|
|
if ( empty( $term_id ) ) { |
|
196
|
|
|
// The term doesn't exists, so create it. |
|
197
|
|
|
$maybe_term = wp_insert_term( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
198
|
|
|
} else { |
|
199
|
|
|
// Get the term. |
|
200
|
|
|
$maybe_term = get_term( $term_id['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ARRAY_A ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
// Check for errors. |
|
204
|
|
|
if ( is_wp_error( $maybe_term ) ) { |
|
205
|
|
|
$this->log->info( 'wl_install_entity_type_data [ ' . $maybe_term->get_error_message() . ' ]' ); |
|
206
|
|
|
return false; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Finally return the term id. |
|
210
|
|
|
return $maybe_term['term_id']; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state