Completed
Push — master ( 0c53d4...debbec )
by David
09:08 queued 10s
created

Wordlift_Install_1_0_0::install()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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, ) );
0 ignored issues
show
Documentation introduced by
array('key' => $key) is of type array<string,string,{"key":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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