Completed
Push — develop ( d39ea9...44b932 )
by David
04:36 queued 10s
created

Wordlift_Install_3_18_3   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 0 3 1
A set_article_term_to_posts() 0 49 3
1
<?php
2
/**
3
 * Installs: Install Version 3.18.3.
4
 *
5
 * @since      3.18.3
6
 * @package    Wordlift
7
 * @subpackage Wordlift/install
8
 */
9
10
/**
11
 * Define the {@link Wordlift_Install_3_18_3} interface.
12
 *
13
 * @since      3.18.3
14
 * @package    Wordlift
15
 * @subpackage Wordlift/install
16
 */
17
class Wordlift_Install_3_18_3 extends Wordlift_Install {
18
	/**
19
	 * @inheritdoc
20
	 */
21
	protected static $version = '3.18.3';
22
23
	/**
24
	 * @inheritdoc
25
	 */
26
	public function install() {
27
		$this->set_article_term_to_posts();
28
	}
29
30
	/**
31
	 * Set default article term to posts
32
	 * that exists in `wl_relation_instances` table.
33
	 *
34
	 * @since 3.18.3
35
	 *
36
	 * @return mixed False if the `article` doesn't exists.
37
	 */
38
	public function set_article_term_to_posts() {
39
		// Load the global $wpdb;
40
		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...
41
42
		// Get the article term.
43
		$term = get_term_by(
44
			'slug',
45
			'article',
46
			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
47
		);
48
49
		// Bail if term doesn't exist.
50
		if ( empty( $term ) ) {
51
			return false;
52
		}
53
54
		// Set `article` term to all posts that exists in
55
		// `wl_relation_instances` table and don't have `article` term set.
56
		$post_ids = $wpdb->get_results(
57
			$wpdb->prepare(
58
				"
59
				SELECT DISTINCT p.ID
60
				FROM $wpdb->posts AS p
61
				INNER JOIN {$wpdb->prefix}wl_relation_instances AS ri
62
					ON p.ID = ri.subject_id
63
				WHERE p.post_status = 'publish'
64
				AND p.post_type = '%s'
65
				AND (
66
					p.ID NOT IN (
67
						SELECT object_id
68
						FROM $wpdb->term_relationships
69
						WHERE term_taxonomy_id IN (%d)
70
					)
71
				)
72
				",
73
				'post',
74
				$term->term_id
75
			)
76
		);
77
78
		// Loop through all posts and set `article` term for each one.
79
		foreach ($post_ids as $p) {
80
			wp_set_object_terms(
81
				(int) $p->ID,
82
				$term->term_id,
83
				Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME
84
			);
85
		}
86
	}
87
88
}
89