Completed
Branch FET/paypal-smart-button2 (052e8d)
by
unknown
102:07 queued 88:11
created

PrivacyPolicyManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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_Screen;
15
16
/**
17
 * Class PrivacyPolicyManager
18
 * Manages setting up the hooks to add the EE core and add-ons' privacy policies
19
 *
20
 * @package        Event Espresso
21
 * @author         Mike Nelson
22
 * @since          4.9.62.p
23
 */
24
class PrivacyPolicyManager
25
{
26
27
    public function __construct()
28
    {
29
        add_action('current_screen', array($this, 'addPrivacyPolicy'), 9);
30
    }
31
32
33
    /**
34
     * For all the registered `PrivacyPolicyInterface`s, add their privacy policy content
35
     *
36
     * @param WP_Screen $screen
37
     * @throws InvalidClassException
38
     * @throws InvalidDataTypeException
39
     * @throws InvalidEntityException
40
     * @throws InvalidFilePathException
41
     * @throws InvalidIdentifierException
42
     * @throws InvalidInterfaceException
43
     */
44
    public function addPrivacyPolicy(WP_Screen $screen)
45
    {
46
        if ($screen instanceof WP_Screen
0 ignored issues
show
Bug introduced by
The class WP_Screen 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...
47
            && $screen->id === 'tools'
48
            && isset($_GET['wp-privacy-policy-guide'])) {
49
            // load all the privacy policy stuff
50
            // add post policy text
51
            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...
52
                wp_add_privacy_policy_content($privacy_policy->getName(), $privacy_policy->getContent());
53
            }
54
        }
55
    }
56
57
58
    /**
59
     * @return CollectionInterface|PrivacyPolicyInterface[]
60
     * @throws InvalidIdentifierException
61
     * @throws InvalidInterfaceException
62
     * @throws InvalidFilePathException
63
     * @throws InvalidEntityException
64
     * @throws InvalidDataTypeException
65
     * @throws InvalidClassException
66
     */
67
    protected function loadPrivacyPolicyCollection()
68
    {
69
        $loader = new CollectionLoader(
70
            new CollectionDetails(
71
                // collection name
72
                'privacy_policies',
73
                // collection interface
74
                'EventEspresso\core\services\privacy\policy\PrivacyPolicyInterface',
75
                // FQCNs for classes to add (all classes within that namespace will be loaded)
76
                apply_filters(
77
                    'FHEE__EventEspresso_core_services_privacy_policy_PrivacyPolicyManager__privacy_policies',
78
                    array('EventEspresso\core\domain\services\admin\privacy\policy\PrivacyPolicy')
79
                ),
80
                // filepaths to classes to add
81
                array(),
82
                // file mask to use if parsing folder for files to add
83
                '',
84
                // what to use as identifier for collection entities
85
                // using CLASS NAME prevents duplicates (works like a singleton)
86
                CollectionDetails::ID_CLASS_NAME
87
            )
88
        );
89
        return $loader->getCollection();
90
    }
91
}
92
// End of file PrivacyPolicyManager.php
93
// Location: EventEspresso\core\domain\services\admin/PrivacyPolicyManager.php
94