Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SecondaryType
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 instantiate_from_option
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace NewfoldLabs\WP\Module\Data\SiteClassification;
4
5/**
6 * Class SecondaryType
7 *
8 * Class that manages the secondary site classification type.
9 *
10 * @package NewfoldLabs\WP\Module\Data
11 */
12final class SecondaryType extends Types {
13    /**
14     * Name of the site classification secondary option.
15     *
16     * @var string
17     */
18    public static $secondary_option_name = 'nfd_data_site_classification_secondary';
19
20    /**
21     * Constructor for the SecondaryType class.
22     *
23     * @param string $refers Indicates what the value refers to, slug(from default slugs)|custom(from a custom input field).
24     * @param string $value The actual value of the site classification type.
25     */
26    public function __construct( $refers, $value ) {
27        parent::__construct( self::$secondary_option_name, $refers, $value );
28    }
29
30    /**
31     * Validates the data.
32     *
33     * @return boolean
34     */
35    public function validate() {
36        // If the secondary type refers to a custom value (from a user input field) we cannot validate the value.
37        if ( 'custom' === $this->refers ) {
38            return true;
39        }
40
41        // Retrieve the selected primary type.
42        $primary = PrimaryType::instantiate_from_option();
43        // If it does not exist, then give benefit of doubt.
44        if ( ! ( $primary instanceof PrimaryType ) ) {
45            return true;
46        }
47
48        $classification  = SiteClassification::get();
49        $secondary_types = $classification['types'][ $primary->value ]['secondaryTypes'];
50        // If secondaryTypes does not exist or the selected slug does not exist then return false.
51        if ( ! isset( $secondary_types ) || ! isset( $secondary_types[ $this->value ] ) ) {
52            return false;
53        }
54
55        return true;
56    }
57
58    /**
59     * Instantiates a class object from the data stored in the option.
60     *
61     * @return SecondaryType|boolean
62     */
63    public static function instantiate_from_option() {
64        $data = get_option( self::$secondary_option_name, false );
65
66        if ( ! $data || ! is_array( $data ) || ! isset( $data['refers'] ) || ! isset( $data['value'] ) ) {
67            delete_option( self::$secondary_option_name );
68            return false;
69        }
70
71        $instance = new self( $data['refers'], $data['value'] );
72        if ( ! $instance->validate() ) {
73            delete_option( self::$secondary_option_name );
74            return false;
75        }
76
77        return $instance;
78    }
79}