Completed
Branch GDPR/privacy-policy-hook (e221f0)
by
unknown
60:30 queued 46:10
created

PrivacyPolicyManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\privacy\policy;
4
5
use EventEspresso\core\exceptions\InvalidClassException;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
use EventEspresso\core\exceptions\InvalidEntityException;
8
use EventEspresso\core\exceptions\InvalidFilePathException;
9
use EventEspresso\core\exceptions\InvalidIdentifierException;
10
use EventEspresso\core\exceptions\InvalidInterfaceException;
11
use EventEspresso\core\services\collections\CollectionDetails;
12
use EventEspresso\core\services\collections\CollectionInterface;
13
use EventEspresso\core\services\collections\CollectionLoader;
14
use WP_Post;
15
16
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed');
17
18
19
20
/**
21
 * Class PrivacyPolicyManager
22
 * Manages setting up the hooks to add the EE core and add-ons' privacy policies
23
 *
24
 * @package        Event Espresso
25
 * @author         Mike Nelson
26
 * @since          $VID:$
27
 */
28
class PrivacyPolicyManager
29
{
30
31
    public function __construct()
32
    {
33
        if (function_exists('wp_add_privacy_policy_content')) {
34
            add_action('edit_form_after_title', array($this, 'addPrivacyPolicy'), 9);
35
        }
36
    }
37
38
39
40
    /**
41
     * For all the registered `PrivacyPolicyInterface`s, add their privacy policy content
42
     *
43
     * @param WP_Post $post
44
     */
45
    public function addPrivacyPolicy($post)
46
    {
47
        if (! ($post instanceof \WP_Post)) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
48
            return;
49
        }
50
        $policy_page_id = (int)get_option('wp_page_for_privacy_policy');
51
        if (! $policy_page_id || $policy_page_id != $post->ID) {
52
            return;
53
        }
54
        //load all the privacy policy stuff
55
        //add post policy text
56
        foreach ($this->loadPrivacyPolicyCollection() as $privacy_policy) {
0 ignored issues
show
Bug introduced by
The expression $this->loadPrivacyPolicyCollection() of type object<EventEspresso\cor...rivacyPolicyInterface>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
57
            wp_add_privacy_policy_content($privacy_policy->getName(), $privacy_policy->getContent());
58
        }
59
    }
60
61
62
63
    /**
64
     * @return CollectionInterface|PrivacyPolicyInterface[]
65
     * @throws InvalidIdentifierException
66
     * @throws InvalidInterfaceException
67
     * @throws InvalidFilePathException
68
     * @throws InvalidEntityException
69
     * @throws InvalidDataTypeException
70
     * @throws InvalidClassException
71
     */
72
    protected function loadPrivacyPolicyCollection()
73
    {
74
        $loader = new CollectionLoader(
75
            new CollectionDetails(
76
            // collection name
77
                'privacy_policies',
78
                // collection interface
79
                'EventEspresso\core\services\privacy\policy\PrivacyPolicyInterface',
80
                // FQCNs for classes to add (all classes within that namespace will be loaded)
81
                apply_filters(
82
                    'FHEE__EventEspresso_core_services_privacy_policy_PrivacyPolicyManager__privacy_policies',
83
                    array('EventEspresso\core\domain\services\admin\privacy\policy\PrivacyPolicy')
84
                ),
85
                // filepaths to classes to add
86
                array(),
87
                // file mask to use if parsing folder for files to add
88
                '',
89
                // what to use as identifier for collection entities
90
                // using CLASS NAME prevents duplicates (works like a singleton)
91
                CollectionDetails::ID_CLASS_NAME
92
            )
93
        );
94
        return $loader->getCollection();
95
    }
96
97
98
99
}
100
// End of file PrivacyPolicyManager.php
101
// Location: EventEspresso\core\domain\services\admin/PrivacyPolicyManager.php