Plugin Directory

source: polylang/trunk/src/Options/Inactive_Option.php

Last change on this file was 3467776, checked in by Chouby, 5 weeks ago

Version 3.8-beta2 in trunk

File size: 2.6 KB
Line 
1<?php
2/**
3 * @package Polylang
4 */
5
6namespace WP_Syntex\Polylang\Options;
7
8use WP_Error;
9use WP_Syntex\Polylang\Options\Options;
10
11defined( 'ABSPATH' ) || exit;
12
13/**
14 * Class defining a decorator for options when Polylang is not active on the current site.
15 *
16 * @since 3.8
17 */
18class Inactive_Option extends Abstract_Option {
19        public const ERROR_CODE = 'pll_not_active';
20
21        /**
22         * The option to decorate.
23         *
24         * @var Abstract_Option
25         */
26        private $option;
27
28        /**
29         * The key of the option to decorate.
30         *
31         * @var string
32         *
33         * @phpstan-var non-falsy-string
34         */
35        private static $key = 'not-an-option';
36
37        /**
38         * Constructor.
39         *
40         * @since 3.8
41         *
42         * @param Abstract_Option $option The option to wrap.
43         */
44        public function __construct( Abstract_Option $option ) {
45                $this->option = $option;
46                $this->errors = new WP_Error();
47
48                // Make sure the option doesn't contain any value.
49                $this->option->reset();
50        }
51
52        /**
53         * Returns option key.
54         *
55         * @since 3.8
56         *
57         * @return string
58         *
59         * @phpstan-return non-falsy-string
60         */
61        public static function key(): string {
62                return self::$key;
63        }
64
65        /**
66         * Does nothing except adding an error.
67         *
68         * @since 3.8
69         *
70         * @param mixed   $value   Value to set.
71         * @param Options $options All options.
72         * @return bool True if the value has been assigned. False in case of errors.
73         */
74        public function set( $value, Options $options ): bool { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
75                if ( ! in_array( self::ERROR_CODE, $this->errors->get_error_codes(), true ) ) {
76                        $this->errors->add(
77                                self::ERROR_CODE,
78                                /* translators: %s is a blog ID. */
79                                sprintf( __( 'Polylang is not active on site %s.', 'polylang' ), (int) get_current_blog_id() )
80                        );
81                }
82                return false;
83        }
84
85        /**
86         * Returns the value of the option, usually the default value for inactive options.
87         *
88         * @since 3.8
89         *
90         * @return mixed
91         */
92        public function get() {
93                return $this->option->get();
94        }
95
96        /**
97         * Returns an empty schema.
98         *
99         * @since 3.8
100         *
101         * @return array The schema.
102         */
103        public function get_schema(): array {
104                return array();
105        }
106
107        /**
108         * Returns the default value.
109         *
110         * @since 3.8
111         *
112         * @return mixed
113         */
114        protected function get_default() {
115                return $this->option->get();
116        }
117
118        /**
119         * Not used but required by `Abstract_Option`.
120         *
121         * @since 3.8
122         *
123         * @return array Partial schema.
124         */
125        protected function get_data_structure(): array {
126                return array();
127        }
128
129        /**
130         * Not used but required by `Abstract_Option`.
131         *
132         * @since 3.8
133         *
134         * @return string
135         */
136        protected function get_description(): string {
137                return '';
138        }
139}
Note: See TracBrowser for help on using the repository browser.