Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PrimaryType
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
132
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 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 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 PrimaryType
7 *
8 * Class that manages the primary site classification type.
9 *
10 * @package NewfoldLabs\WP\Module\Data
11 */
12final class PrimaryType extends Types {
13    /**
14     * Name of the site classification primary option.
15     *
16     * @var string
17     */
18    public static $primary_option_name = 'nfd_data_site_classification_primary';
19
20    /**
21     * Constructor for PrimaryType.
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::$primary_option_name, $refers, $value );
28    }
29
30    /**
31     * Validates the data.
32     *
33     * @return boolean
34     */
35    public function validate() {
36        // If the primary 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 data to validate.
42        $classification = SiteClassification::get();
43        // Checks if the value is a valid primary type slug.
44        if ( ! isset( $classification['types'] ) || ! isset( $classification['types'][ $this->value ] ) ) {
45            return false;
46        }
47
48        return true;
49    }
50
51    /**
52     * Instantiates a class object from the data stored in the option.
53     *
54     * @return PrimaryType|boolean
55     */
56    public static function instantiate_from_option() {
57        $data = get_option( self::$primary_option_name, false );
58
59        if ( ! $data || ! is_array( $data ) || ! isset( $data['refers'] ) || ! isset( $data['value'] ) ) {
60            delete_option( self::$primary_option_name );
61            return false;
62        }
63
64        $instance = new self( $data['refers'], $data['value'] );
65        if ( ! $instance->validate() ) {
66            delete_option( self::$primary_option_name );
67            return false;
68        }
69
70        return $instance;
71    }
72}