├── LICENSE ├── README.md ├── composer.json └── src └── class-wp-options-page.php /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WP Options Page 2 | 3 | A class to build options pages for your WordPress plugins and themes. 4 | 5 | ## Install 6 | 7 | Install using composer: 8 | 9 | ``` 10 | composer require luizbills/wp-options-page 11 | ``` 12 | 13 | ## Getting Started 14 | 15 | Just create an `WP_Options_Page` class instance on `init` action hook: 16 | 17 | ```php 18 | function yourprefix_create_settings_page () { 19 | $page = new WP_Options_Page(); 20 | 21 | // give your page a ID 22 | $page->id = 'my_settings_page'; 23 | 24 | // set the menu name 25 | $page->menu_title = 'My Settings'; 26 | 27 | // register your options fields 28 | $page->fields = [ 29 | // a simple text input field 30 | [ 31 | 'id' => 'api_key', 32 | 'title' => 'API Key', 33 | 'type' => 'text', 34 | ] 35 | ]; 36 | 37 | // register the page 38 | $page->init(); 39 | 40 | // access the stored options 41 | $api_key = $page->get_option( 'api_key' ); 42 | 43 | // store this page in a global object or variable 44 | // So you can easily your instance class later 45 | // example: My_Plugin->settings = $page; 46 | } 47 | add_action( 'init', 'yourprefix_create_settings_page' ); 48 | ``` 49 | 50 | Or create your own derived class: 51 | 52 | ```php 53 | class My_Settings_Page extends WP_Options_Page { 54 | // I recommend using Singleton pattern 55 | // So you can easily retrieve the class later 56 | // example: My_Settings_Page::instance()->get_option( 'api_key' ); 57 | private static $instance = null; 58 | public static function instance () { 59 | if ( ! self::$instance ) self::$instance = new self(); 60 | return self::$instance; 61 | } 62 | 63 | private function __construct () { 64 | add_action( 'init', [ $this, 'init' ] ); 65 | } 66 | 67 | // overrides the `init` method to setup your page 68 | public function init () { 69 | // give your page a ID 70 | $this->id = 'my_settings_page'; 71 | 72 | // set the menu name 73 | $this->menu_title = 'My Settings'; 74 | 75 | // register the page 76 | parent::init(); 77 | } 78 | 79 | // overrides the `get_fields` method to register your fields 80 | public function get_fields () { 81 | return [ 82 | [ 83 | 'id' => 'api_key', 84 | 'title' => 'API Key', 85 | 'type' => 'text', 86 | ] 87 | ]; 88 | } 89 | } 90 | 91 | // start your class 92 | My_Settings_Page::instance(); 93 | ``` 94 | 95 | Preview: 96 | 97 | ![](https://user-images.githubusercontent.com/1798830/215272911-9a90f0fd-d62d-49f4-bc64-7906f513695a.png) 98 | 99 | ## Learn More 100 | 101 | Visit our [wiki](https://github.com/luizbills/wp-options-page/wiki) or study (and install) our [Demo Plugin](https://github.com/luizbills/wp-options-page-demo). 102 | 103 | ## LICENSE 104 | 105 | GPLv2 or later 106 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "luizbills/wp-options-page", 3 | "description": "Easy way to build options/settings pages in your WordPress plugins and themes.", 4 | "keywords": ["wordpress", "options", "settings", "library"], 5 | "homepage": "https://github.com/luizbills/wp-options-page", 6 | "license": "GPL-2.0-or-later", 7 | "authors": [ 8 | { 9 | "name": "Luiz Bills", 10 | "email": "luizbills@pm.me", 11 | "homepage": "https://www.luizpb.com/", 12 | "role": "Developer" 13 | } 14 | ], 15 | "support": { 16 | "issues": "https://github.com/luizbills/wp-options-page/issues", 17 | "docs": "https://github.com/luizbills/wp-options-page/wiki" 18 | }, 19 | "funding": [ 20 | { 21 | "type": "donate", 22 | "url": "https://www.luizpb.com/donate/" 23 | } 24 | ], 25 | "autoload": { 26 | "files": ["src/class-wp-options-page.php"] 27 | }, 28 | "require": { 29 | "php": ">=7.4" 30 | }, 31 | "require-dev": { 32 | "szepeviktor/phpstan-wordpress": "^1.1", 33 | "phpstan/phpstan": "^1.10" 34 | }, 35 | "scripts": { 36 | "check": "phpstan analyse" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/class-wp-options-page.php: -------------------------------------------------------------------------------- 1 | 8 | * @version 0.8.0 9 | * @see https://github.com/luizbills/wp-options-page 10 | */ 11 | class WP_Options_Page { 12 | /** 13 | * The package version. 14 | * 15 | * @since 0.6.0 16 | * @var string 17 | */ 18 | const VERSION = '0.8.0'; 19 | 20 | /** 21 | * The ID (also the slug) of the page. Should be unique for this menu page and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with `sanitize_key()`. 22 | * 23 | * @since 0.1.0 24 | * @see https://developer.wordpress.org/reference/functions/sanitize_key/ 25 | * @var string 26 | */ 27 | public $id = null; 28 | 29 | /** 30 | * The slug name for the parent menu (or the file name of a standard WordPress admin page). 31 | * 32 | * @since 0.1.0 33 | * @var string|null 34 | */ 35 | public $menu_parent = null; 36 | 37 | /** 38 | * The text to be displayed in the tag of the page when the menu is selected. 39 | * This text is also used in a <h1> tag at the top of the page, if the $insert_title is equal to `true`. 40 | * 41 | * @since 0.1.0 42 | * @see WP_Options_Page::$insert_title 43 | * @var string|null 44 | */ 45 | public $page_title = null; 46 | 47 | /** 48 | * A text that appears immediately after the <h1> title of the page, if the $insert_title is equal to `true`. 49 | * 50 | * @since 0.1.0 51 | * @see WP_Options_Page::$insert_title 52 | * @var string|null 53 | */ 54 | public $page_description = null; 55 | 56 | /** 57 | * If enabled, inserts a <h1> tag title and description at the top of the page. 58 | * 59 | * @since 0.1.0 60 | * @see WP_Options_Page::$page_description 61 | * @see WP_Options_Page::$page_title 62 | * @var bool 63 | */ 64 | public $insert_title = true; 65 | 66 | /** 67 | * The text to be used for the menu. 68 | * 69 | * @since 0.1.0 70 | * @var string|null 71 | */ 72 | public $menu_title = null; 73 | 74 | /** 75 | * The position in the menu order this item should appear. 76 | * 77 | * @since 0.1.0 78 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/#parameters 79 | * @var int|float 80 | */ 81 | public $menu_position = null; 82 | 83 | /** 84 | * The hook priority used if this page is a subpage. 85 | * 86 | * @since 0.1.0 87 | * @var int|float 88 | */ 89 | public $menu_priority = 10; 90 | 91 | /** 92 | * The URL to the icon to be used for this menu. 93 | * - Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. This should begin with 'data:image/svg+xml;base64,'. 94 | * - Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-chart-pie'. 95 | * - Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. 96 | * 97 | * @since 0.1.0 98 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/#parameters 99 | * @var string 100 | */ 101 | public $menu_icon = 'dashicons-admin-generic'; 102 | 103 | /** 104 | * The capability (or user role) required for this menu to be displayed to the user. 105 | * 106 | * @since 0.1.0 107 | * @var string 108 | */ 109 | public $capability = 'manage_options'; 110 | 111 | /** 112 | * The option name where all options of the page will be stored. 113 | * By default is `"{$this->id}_options"`. 114 | * 115 | * @since 0.1.0 116 | * @var string|null 117 | */ 118 | public $option_name = null; 119 | 120 | /** 121 | * The prefix appended in all field name attributes. 122 | * By default is `"{$this->id}_"`. 123 | * 124 | * @since 0.1.0 125 | * @var string|null 126 | */ 127 | public $field_prefix = null; 128 | 129 | /** 130 | * The prefix appended in all hooks triggered by the page. 131 | * By default is `"{$this->field_prefix}_"`. 132 | * 133 | * @since 0.1.0 134 | * @see WP_Options_Page::add_action() 135 | * @see WP_Options_Page::add_filter() 136 | * @var string|null 137 | */ 138 | protected $hook_prefix = null; 139 | 140 | /** 141 | * The page's hook_suffix returned by `add_menu_page()` or `add_submenu_page()`. 142 | * 143 | * @since 0.1.0 144 | * @see WP_Options_Page::get_hook_suffix() 145 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/#return 146 | * @var string 147 | */ 148 | protected $hook_suffix = null; 149 | 150 | /** 151 | * The fields of the page. 152 | * 153 | * @since 0.1.0 154 | * @see WP_Options_Page::init_fields() 155 | * @var array 156 | */ 157 | public $fields = []; 158 | 159 | /** 160 | * Array with some strings that are used on the page. You can overwrite them to change or make them translatable. 161 | * 162 | * @since 0.1.0 163 | * @var string[] 164 | */ 165 | public $strings = []; 166 | 167 | /** 168 | * @since 0.1.0 169 | * @see WP_Options_Page::add_notice() 170 | * @var array 171 | */ 172 | protected $admin_notices = []; 173 | 174 | /** 175 | * A flag used during the page rendering process. 176 | * 177 | * @since 0.1.0 178 | * @see WP_Options_Page::render_field() 179 | * @see WP_Options_Page::maybe_open_or_close_table() 180 | * @var bool 181 | */ 182 | protected $table_is_open = false; 183 | 184 | /** 185 | * @since 0.1.0 186 | * @see WP_Options_Page::add_script() 187 | * @var array 188 | */ 189 | protected $scripts = []; 190 | 191 | /** 192 | * @since 0.1.0 193 | * @see WP_Options_Page::add_style() 194 | * @var array 195 | */ 196 | protected $styles = []; 197 | 198 | /** 199 | * The default value of each field of the page. 200 | * 201 | * @since 0.1.0 202 | * @see WP_Options_Page::init_fields() 203 | * @var array 204 | */ 205 | protected $default_values = []; 206 | 207 | /** 208 | * List of supported features. 209 | * The intention is that it will be used by other developers to choose whether or not to activate a feature. 210 | * 211 | * @since 0.3.0 212 | * @var array<string|int,mixed> 213 | */ 214 | public $supports = []; 215 | 216 | /** 217 | * List of attributes of the <form> tag. 218 | * 219 | * @since 0.3.0 220 | * @var array<string, mixed> 221 | */ 222 | public $form_attributes = []; 223 | 224 | /** 225 | * @since 0.3.0 226 | * @var boolean 227 | */ 228 | public $credits = true; 229 | 230 | /** 231 | * @since 0.1.0 232 | * @return void 233 | */ 234 | public function init () { 235 | if ( ! \did_action( 'init' ) ) { 236 | throw new \Exception( 'Please, don\'t use the ' . \get_class( $this ) . ' class before "init" hook.' ); 237 | } 238 | if ( ! $this->id ) { 239 | throw new \Exception( 'Missing $id in ' . \get_class( $this ) ); 240 | } 241 | 242 | $this->menu_title = $this->menu_title ?? $this->id; 243 | $this->page_title = $this->page_title ?? $this->menu_title; 244 | $this->option_name = $this->option_name ?? $this->id . '_options'; 245 | $this->field_prefix = $this->field_prefix ?? $this->id . '_'; 246 | $this->hook_prefix = $this->hook_prefix ?? $this->field_prefix; 247 | 248 | $default_strings = [ 249 | 'template_notice_error' => '<strong>Error</strong>: %s', 250 | 'checkbox_enable' => 'Enable', 251 | 'options_updated' => '<strong>' . \esc_html__( 'Settings saved.' ) . '</strong>', 252 | 'submit_button_label' => \esc_html__( 'Save Changes' ), 253 | ]; 254 | $this->strings = \array_replace( $default_strings, (array) $this->strings); 255 | 256 | $this->form_attributes = (array) $this->form_attributes; 257 | 258 | \do_action( 'wp_options_page_init', $this ); 259 | 260 | $this->init_hooks(); 261 | $this->init_fields(); 262 | $this->handle_options(); 263 | } 264 | 265 | /** 266 | * @since 0.1.0 267 | * @return void 268 | */ 269 | protected function init_hooks () { 270 | // register admin page 271 | \add_action( 'admin_menu', [ $this, 'add_menu_page' ], $this->menu_priority ); 272 | 273 | // enqueue admin page JS and CSS files 274 | \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); 275 | 276 | // maybe show "Settings saved" on submit 277 | $this->add_action( 'after_update_options', [ $this, 'add_settings_saved_notice' ], 10, 2 ); 278 | 279 | // Removes slashes from a $_POST field values 280 | $this->add_filter( 'posted_value', 'wp_unslash' ); 281 | 282 | $this->do_action( 'after_init_hooks', $this ); 283 | } 284 | 285 | /** 286 | * @since 0.1.0 287 | * @return void 288 | */ 289 | public function init_fields () { 290 | $this->fields = $this->apply_filters( 291 | 'get_fields', 292 | ! empty( $this->fields ) ? $this->fields : $this->get_fields(), 293 | $this 294 | ); 295 | 296 | foreach ( $this->fields as $field ) { 297 | $id = $field['id'] ?? false; 298 | if ( ! empty( $id ) ) { 299 | $this->default_values[ $id ] = $field['default'] ?? null; 300 | } 301 | } 302 | 303 | if ( $this->insert_title ) { 304 | $primary_title = $this->apply_filters( 305 | 'top_page_title', 306 | [ 307 | 'type' => 'title', 308 | 'title' => $this->page_title, 309 | 'tag' => 'h1', 310 | 'description' => $this->page_description, 311 | 'class' => 'page-title-top', 312 | ], 313 | $this 314 | ); 315 | if ( $primary_title ) { 316 | \array_unshift( $this->fields, $primary_title ); 317 | } 318 | } 319 | 320 | $has_submit = false; 321 | foreach ( $this->fields as $key => $field ) { 322 | $field = $this->prepare_field( $field ); 323 | if ( ! $field ) continue; 324 | 325 | $this->fields[ $key ] = $field; 326 | 327 | if ( ! $has_submit && 'submit' === $field['type'] ) { 328 | $has_submit = true; 329 | } 330 | } 331 | 332 | if ( ! $has_submit ) { 333 | $this->fields[] = $this->prepare_field( [ 334 | 'type' => 'submit' 335 | ] ); 336 | } 337 | 338 | $this->do_action( 'after_init_fields', $this ); 339 | } 340 | 341 | /** 342 | * @since 0.1.0 343 | * @return void 344 | */ 345 | public function add_menu_page () { 346 | if ( ! $this->menu_parent ) { 347 | $this->hook_suffix = \add_menu_page( 348 | $this->page_title, 349 | $this->menu_title, 350 | $this->capability, 351 | $this->id, 352 | [ $this, 'render_page' ], 353 | $this->menu_icon, 354 | $this->menu_position 355 | ); 356 | } else { 357 | $this->hook_suffix = \add_submenu_page( 358 | $this->menu_parent, 359 | $this->page_title, 360 | $this->menu_title, 361 | $this->capability, 362 | $this->id, 363 | [ $this, 'render_page' ], 364 | $this->menu_position 365 | ); 366 | } 367 | 368 | $this->do_action( 'admin_menu', $this, $this->hook_suffix ); 369 | } 370 | 371 | /** 372 | * @since 0.3.0 373 | * @return string 374 | */ 375 | public function get_hook_suffix () { 376 | return $this->hook_suffix; 377 | } 378 | 379 | /** 380 | * @since 0.1.0 381 | * @return array 382 | */ 383 | public function get_fields () { 384 | return [ 385 | [ 386 | 'type' => 'title', 387 | 'tag' => 'h2', 388 | 'title' => 'Hey!', 389 | 'description' => 'You not defined any field for this page yet.', 390 | ], 391 | ]; 392 | } 393 | 394 | /** 395 | * @since 0.1.0 396 | * @param array $field 397 | * @return mixed 398 | */ 399 | public function get_field_value ( $field ) { 400 | if ( isset( $field['value'] ) ) { 401 | return $field['value']; 402 | } 403 | return $this->apply_filters( 404 | 'get_field_value', 405 | $this->get_option( $field['id'] ), 406 | $field, 407 | $this 408 | ); 409 | } 410 | 411 | /** 412 | * @since 0.1.0 413 | * @param string $field_id 414 | * @return mixed 415 | */ 416 | public function get_field_default_value ( $field_id ) { 417 | return $this->apply_filters( 418 | 'get_field_default_value', 419 | $this->default_values[ $field_id ] ?? false, 420 | $field_id, 421 | $this 422 | ); 423 | } 424 | 425 | /** 426 | * @since 0.1.0 427 | * @param string $field_id 428 | * @return mixed 429 | */ 430 | public function get_option ( $field_id ) { 431 | $default = $this->get_field_default_value( $field_id ); 432 | $options = \get_option( $this->option_name, [] ); 433 | return $options[ $field_id ] ?? $default; 434 | } 435 | 436 | /** 437 | * @since 0.1.0 438 | * @param array $field 439 | * @return string 440 | */ 441 | public function get_field_name ( $field ) { 442 | return $this->apply_filters( 443 | 'get_field_name', 444 | $this->field_prefix . $field['id'], 445 | $field, 446 | $this 447 | ); 448 | } 449 | 450 | /** 451 | * @since 0.1.0 452 | * @param string $handle 453 | * @param string $src 454 | * @param string[] $deps 455 | * @param string|bool|null $ver 456 | * @param bool $in_footer 457 | * @return void 458 | */ 459 | public function add_script ( $handle, $src = '', $deps = [], $ver = false, $in_footer = false ) { 460 | $this->scripts[] = [ $handle, $src, $deps, $ver, $in_footer ]; 461 | } 462 | 463 | /** 464 | * @since 0.1.0 465 | * @param string $handle 466 | * @param string $src 467 | * @param string[] $deps 468 | * @param string|bool|null $ver = false 469 | * @param string $media 470 | * @return void 471 | */ 472 | public function add_style ( $handle, $src = '', $deps = [], $ver = false, $media = 'all' ) { 473 | $this->styles[] = [ $handle, $src, $deps, $ver, $media ]; 474 | } 475 | 476 | /** 477 | * @since 0.1.0 478 | * @return string 479 | */ 480 | public function get_nonce_action () { 481 | return $this->field_prefix . 'nonce'; 482 | } 483 | 484 | /** 485 | * @since 0.1.0 486 | * @return string 487 | */ 488 | public function get_nonce_name () { 489 | return '_nonce'; 490 | } 491 | 492 | /** 493 | * @since 0.1.0 494 | * @param string $message 495 | * @param string $type Should be "error", "success", "warning" or "info". 496 | * @param string $class 497 | * @return void 498 | */ 499 | public function add_notice ( $message, $type = 'info', $class = '' ) { 500 | $this->admin_notices[] = [ 501 | 'message' => $message, 502 | 'type' => $type, 503 | 'class' => $class, 504 | ]; 505 | } 506 | 507 | /** 508 | * @since 0.5.0 509 | * @param string $message 510 | * @param array $field 511 | * @return void 512 | */ 513 | public function add_error ( $message, $field ) { 514 | $notice = \sprintf( $this->strings['template_notice_error'], $message ); 515 | $notice = $this->apply_filters( 'get_error_notice', $notice, $field, $message, $this ); 516 | if ( ! empty( $notice ) ) { 517 | $this->add_notice( $notice, 'error' ); 518 | } 519 | } 520 | 521 | /** 522 | * @since 0.1.0 523 | * @param string $hook_suffix 524 | * @return void 525 | */ 526 | public function enqueue_scripts ( $hook_suffix ) { 527 | if ( $this->hook_suffix !== $hook_suffix ) return; 528 | foreach ( $this->scripts as $params ) { 529 | \call_user_func_array( 'wp_enqueue_script', $params ); 530 | } 531 | foreach ( $this->styles as $params ) { 532 | \call_user_func_array( 'wp_enqueue_style', $params ); 533 | } 534 | } 535 | 536 | /** 537 | * @since 0.1.0 538 | * @return void 539 | */ 540 | public function handle_options () { 541 | if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) return; 542 | if ( ! \is_admin() ) return; 543 | if ( $this->id !== ( $_REQUEST['page'] ?? '' ) ) return; 544 | 545 | $nonce = $_POST[ $this->get_nonce_name() ] ?? ''; 546 | $action = $this->get_nonce_action(); 547 | $invalid_nonce = ! \wp_verify_nonce( $nonce, $action ); 548 | $invalid_user = ! \current_user_can( $this->capability ); 549 | if ( $invalid_nonce || $invalid_user ) { 550 | \wp_die( \esc_html__( 'Sorry, you are not allowed to access this page.' ), 403 ); 551 | } 552 | $options = []; 553 | $has_errors = false; 554 | 555 | foreach ( $this->fields as &$field ) { 556 | // skip fields that it's has input 557 | if ( ! $field['__is_input'] ) continue; 558 | 559 | $name = $field['name']; 560 | $value = $this->apply_filters( 'posted_value', $_POST[ $name ] ?? '', $name, $this ); 561 | $field['value'] = $value; 562 | $field['error'] = null; 563 | 564 | // handle field sanitization 565 | $sanitize = $field['@sanitize'] ?? null; 566 | if ( \is_callable( $sanitize ) ) { 567 | if ( \is_scalar( $value ) ) { 568 | $value = $sanitize( $value ); 569 | } else { 570 | $value = \maybe_unserialize( $sanitize( \serialize( $value ) ) ); 571 | } 572 | $field['value'] = $value; 573 | } 574 | 575 | // handle field validation 576 | try { 577 | $validate = $field['@validate'] ?? null; 578 | if ( \is_callable( $validate ) ) { 579 | $validate( $value, $field ); 580 | } 581 | $this->do_action( 'validate_field_' . $field['type'], $field, $this ); 582 | $this->do_action( 'validate_field_' . $field['id'], $field, $this ); 583 | } catch ( \Throwable $e ) { 584 | $error_message = $this->apply_filters( 585 | 'get_error_message', 586 | $e->getMessage(), 587 | $e, 588 | $this 589 | ); 590 | $this->add_error( $error_message, $field ); 591 | $field['error'] = $error_message; 592 | $has_errors = true; 593 | continue; 594 | } 595 | 596 | $options[ $name ] = [ 597 | 'id' => $field['id'], 598 | 'value' => $value 599 | ]; 600 | } 601 | 602 | $abort_update = (bool) $this->apply_filters( 'abort_update', $has_errors, $this ); 603 | 604 | if ( $abort_update ) return; 605 | 606 | $options = $this->apply_filters( 'update_options', $options, $this ); 607 | if ( ! empty( $options ) ) { 608 | $updated = $this->update_options( $options ); 609 | $this->do_action( 'after_update_options', $options, $updated, $this ); 610 | } 611 | } 612 | 613 | /** 614 | * @since 0.1.0 615 | * @param array $options 616 | * @return bool 617 | */ 618 | public function update_options ( $options ) { 619 | $prev = \get_option( $this->option_name ); 620 | $values = \is_array( $prev ) ? $prev : []; 621 | foreach ( $options as $data ) { 622 | $values[ $data['id'] ] = $data['value']; 623 | } 624 | return \update_option( $this->option_name, $values ); 625 | } 626 | 627 | /** 628 | * @since 0.5.0 629 | * @param array $options 630 | * @param bool $updated 631 | * @return void 632 | */ 633 | public function add_settings_saved_notice ( $options, $updated ) { 634 | $this->add_notice( 635 | $this->strings['options_updated'], 636 | 'success', 637 | 'is-dismissible ' . ( $updated ? 'options-updated' : '' ), 638 | ); 639 | } 640 | 641 | /** 642 | * @since 0.1.0 643 | * @return string 644 | */ 645 | public function get_url () { 646 | $path = 'admin.php'; 647 | // woocommerce submenu support 648 | if ( $this->menu_parent && 'woocommerce' !== $this->menu_parent ) { 649 | $path = $this->menu_parent; 650 | } 651 | return \admin_url( $path . '?page=' . $this->id ); 652 | } 653 | 654 | /** 655 | * @since 0.1.0 656 | * @return void 657 | */ 658 | public function render_page () { 659 | // force some <form> attributes 660 | $this->form_attributes['method'] = 'POST'; 661 | $this->form_attributes['action'] = \remove_query_arg( '_wp_http_referer' ); 662 | ?> 663 | <div class="wrap <?php \esc_attr( $this->id ); ?>"> 664 | <?php $this->do_action( 'before_render_form', $this ); ?> 665 | 666 | <form <?php echo self::parse_tag_atts( $this->form_attributes ); ?>> 667 | <?php $this->render_notices() ?> 668 | <?php $this->render_nonce() ?> 669 | <?php $this->render_all_fields() ?> 670 | </form> 671 | 672 | <?php $this->do_action( 'after_render_form', $this ); ?> 673 | 674 | <?php $this->render_credits() ?> 675 | </div> 676 | <?php 677 | } 678 | 679 | /** 680 | * @since 0.1.0 681 | * @return void 682 | */ 683 | protected function render_notices () { 684 | foreach ( $this->admin_notices as $notice ) { 685 | $message = $notice['message'] ?? ''; 686 | $type = $notice['type'] ?? 'error'; 687 | $class = $notice['class'] ?? ''; 688 | $page_class = 'options-page-' . $this->id; 689 | \printf( 690 | '<div class="%s inline notice notice-%s %s"><p>%s</p></div>', 691 | \esc_attr( $page_class ), 692 | \esc_attr( $type ), 693 | \esc_attr( $class ), 694 | $message 695 | ); 696 | } 697 | } 698 | 699 | /** 700 | * @since 0.1.0 701 | * @return void 702 | */ 703 | protected function render_nonce () { 704 | \wp_nonce_field( 705 | $this->get_nonce_action(), 706 | $this->get_nonce_name(), 707 | ); 708 | } 709 | 710 | /** 711 | * @since 0.1.0 712 | * @return void 713 | */ 714 | protected function render_all_fields () { 715 | $this->table_is_open = false; 716 | foreach ( $this->fields as $field ) { 717 | $this->render_field( $field ); 718 | } 719 | } 720 | 721 | protected function render_credits () { 722 | if ( ! $this->credits ) return; 723 | 724 | \add_filter( 'update_footer', function () { 725 | return '<em>Powered by <a href="https://github.com/luizbills/wp-options-page" rel="nofollow noopener" target="_blank">WP Options Page</a>.</em>'; 726 | }, \PHP_INT_MAX - 1 ); 727 | } 728 | 729 | /** 730 | * @since 0.1.0 731 | * @param array $field 732 | * @return void 733 | */ 734 | protected function maybe_open_or_close_table ( $field ) { 735 | $is_input = $field['__is_input']; 736 | if ( ! $this->table_is_open ) { 737 | if ( $is_input ) { 738 | echo '<table class="form-table" role="presentation">'; 739 | $this->table_is_open = true; 740 | } 741 | } elseif ( ! $is_input ) { 742 | echo '</table>'; 743 | $this->table_is_open = false; 744 | } 745 | } 746 | 747 | /** 748 | * @since 0.1.0 749 | * @param array $field 750 | * @return array 751 | */ 752 | protected function prepare_field ( $field ) { 753 | $defaults = [ 754 | 'id' => '', 755 | 'type' => 'text', 756 | 'title' => null, 757 | 'description' => null, 758 | 'default' => null, 759 | 'attributes' => [], 760 | '@sanitize' => null, 761 | '@validate' => null, 762 | '__is_input' => true, 763 | ]; 764 | $field = \array_replace( $defaults, $field ); 765 | 766 | switch ( $field['type'] ) { 767 | case 'title': 768 | case 'subtitle': 769 | case 'submit': 770 | $field['__is_input'] = false; 771 | break; 772 | case 'textarea': 773 | $field['@sanitize'] = $field['@sanitize'] ?? 'sanitize_textarea_field'; 774 | break; 775 | default: 776 | $field['@sanitize'] = $field['@sanitize'] ?? 'sanitize_text_field'; 777 | break; 778 | } 779 | 780 | $field = $this->apply_filters( 'prepare_field_' . $field['type'], $field, $this ); 781 | $field = $this->apply_filters( 'prepare_field', $field, $this ); 782 | 783 | $field['id'] = trim( $field['id'] ); 784 | 785 | if ( $field['__is_input'] ) { 786 | $field['name'] = $this->get_field_name( $field ); 787 | if ( 0 === strlen( $field['id'] ) ) { 788 | throw new \Exception( 'Missing field "id" property. All fields must have a string "id".' ); 789 | } 790 | } 791 | 792 | return $field; 793 | } 794 | 795 | /** 796 | * @since 0.1.0 797 | * @param array $field 798 | * @return void 799 | */ 800 | protected function render_field ( $field ) { 801 | $type = $field['type']; 802 | $method = 'render_field_' . $type; 803 | $this->maybe_open_or_close_table( $field ); 804 | $this->do_action( 'before_render_field', $field, $this ); 805 | 806 | if ( $field['__is_input'] ) { 807 | $this->open_wrapper( $field ); 808 | } 809 | 810 | try { 811 | if ( \method_exists( $this, $method ) ) { 812 | $this->$method( $field ); 813 | } else { 814 | \ob_start(); 815 | $this->do_action( 'render_field_' . $type, $field, $this ); 816 | /** @var string */ 817 | $output = trim( \ob_get_clean() ); 818 | if ( '' !== $output ) { 819 | echo $output; 820 | } else { 821 | throw new \Exception( "Invalid field type \"{$field['type']}\"" ); 822 | } 823 | } 824 | } catch ( \Throwable $e ) { 825 | echo '<code>' . \esc_html( $e->getMessage() ) . '</code>'; 826 | } 827 | 828 | if ( $field['__is_input'] ) { 829 | $this->close_wrapper( $field ); 830 | } 831 | 832 | $this->do_action( 'after_render_field', $field, $this ); 833 | } 834 | 835 | /** 836 | * @since 0.1.0 837 | * @param array $field 838 | * @return void 839 | */ 840 | public function open_wrapper ( $field ) { 841 | $id = $field['id']; 842 | $name = $field['name']; 843 | $title = $field['title'] ?? $id; 844 | ?> 845 | <tr> 846 | <th scope="row"> 847 | <label for="<?php echo \esc_attr( $name ); ?>"><?php echo \esc_html( $title ) ?></label> 848 | </th> 849 | <td> 850 | <?php 851 | } 852 | 853 | /** 854 | * @since 0.1.0 855 | * @param array $field 856 | * @return void 857 | */ 858 | public function close_wrapper ( $field ) { 859 | ?> 860 | </td> 861 | </tr> 862 | <?php 863 | } 864 | 865 | /** 866 | * @since 0.1.0 867 | * @param array $field 868 | * @return void 869 | */ 870 | protected function render_field_text ( $field ) { 871 | $name = $field['name']; 872 | $desc = $field['description']; 873 | $value = $this->get_field_value( $field ); 874 | 875 | $atts = $field['attributes'] ?? []; 876 | $atts['type'] = $atts['type'] ?? 'text'; 877 | $atts['id'] = $name; 878 | $atts['name'] = $name; 879 | $atts['value'] = $value; 880 | $atts['class'] = $atts['class'] ?? 'regular-text'; 881 | $atts['placeholder'] = $atts['placeholder'] ?? false; 882 | $atts['aria-describedby'] = $desc ? \esc_attr( $name ) . '-description' : false; 883 | ?> 884 | 885 | <input <?php echo self::parse_tag_atts( $atts ); ?>> 886 | 887 | <?php $this->do_action( 'after_field_input', $field, $this ); ?> 888 | 889 | <?php if ( $desc ) : ?> 890 | <p class="description" id="<?php echo \esc_attr( $name ); ?>-description"><?php echo $desc ?></p> 891 | <?php endif; 892 | } 893 | 894 | /** 895 | * @since 0.1.0 896 | * @param array $field 897 | * @return void 898 | */ 899 | protected function render_field_textarea ( $field ) { 900 | $name = $field['name']; 901 | $desc = $field['description']; 902 | $value = $this->get_field_value( $field ); 903 | 904 | $atts = $field['attributes'] ?? []; 905 | $atts['id'] = $name; 906 | $atts['name'] = $name; 907 | $atts['class'] = $atts['class'] ?? 'large-text'; 908 | $atts['placeholder'] = $atts['placeholder'] ?? false; 909 | $atts['rows'] = $atts['rows'] ?? 5; 910 | $atts['aria-describedby'] = $desc ? \esc_attr( $name ) . '-description' : false; 911 | ?> 912 | 913 | <textarea <?php echo self::parse_tag_atts( $atts ); ?>><?php echo \esc_html( $value ); ?></textarea> 914 | 915 | <?php $this->do_action( 'after_field_input', $field, $this ); ?> 916 | 917 | <?php if ( $desc ) : ?> 918 | <p class="description" id="<?php echo \esc_attr( $name ); ?>-description"><?php echo $desc ?></p> 919 | <?php endif; 920 | } 921 | 922 | /** 923 | * @since 0.1.0 924 | * @param array $field 925 | * @return void 926 | */ 927 | protected function render_field_select ( $field ) { 928 | $name = $field['name']; 929 | $desc = $field['description']; 930 | $value = $this->get_field_value( $field ); 931 | $options = $this->parse_options( $field['options'] ?? [] ); 932 | 933 | $atts = $field['attributes'] ?? []; 934 | $atts['id'] = $name; 935 | $atts['name'] = $name; 936 | $atts['class'] = $atts['class'] ?? 'regular-text'; 937 | $atts['aria-describedby'] = $desc ? \esc_attr( $name ) . '-description' : false; 938 | ?> 939 | 940 | <select <?php echo self::parse_tag_atts( $atts ); ?>> 941 | <?php foreach ( $options as $opt_value => $opt_label ) : ?> 942 | <option value="<?php echo \esc_attr( $opt_value ); ?>" <?php \selected( $opt_value, $value ) ?>><?php echo \esc_html( $opt_label ) ?></option> 943 | <?php endforeach; ?> 944 | </select> 945 | 946 | <?php $this->do_action( 'after_field_input', $field, $this ); ?> 947 | 948 | <?php if ( $desc ) : ?> 949 | <p class="description" id="<?php echo \esc_attr( $name ); ?>-description"><?php echo $desc ?></p> 950 | <?php endif; 951 | } 952 | 953 | /** 954 | * @since 0.1.0 955 | * @param array $field 956 | * @return void 957 | */ 958 | protected function render_field_radio ( $field ) { 959 | $id = $field['id']; 960 | $name = $field['name']; 961 | $title = $field['title'] ?? $id; 962 | $name = $field['name']; 963 | $desc = $field['description']; 964 | $value = $this->get_field_value( $field ); 965 | $options = $this->parse_options( $field['options'] ?? [] ); 966 | 967 | $atts = $field['attributes'] ?? []; 968 | $atts['type'] = 'radio'; 969 | $atts['name'] = $name; 970 | 971 | unset( $atts['id'] ); 972 | unset( $atts['value'] ); 973 | unset( $atts['checked'] ); 974 | ?> 975 | 976 | <fieldset> 977 | <legend class="screen-reader-text"><span><?php echo \esc_html( \strip_tags( $title ) ); ?></span></legend> 978 | 979 | <?php foreach ( $options as $opt_value => $opt_label ) : 980 | $option_id = \esc_attr( $id . '_' . $opt_value ); ?> 981 | <label for="<?php echo \esc_attr( $option_id ) ?>"> 982 | <input <?php echo self::parse_tag_atts( $atts ); ?> id="<?php echo \esc_attr( $option_id ) ?>" value="<?php echo \esc_attr( $opt_value ) ?>" <?php \checked( $opt_value, $value ); ?>> 983 | <span class="option-label"><?php echo $opt_label ?></span> 984 | </label> 985 | <br> 986 | <?php endforeach ?> 987 | 988 | <?php $this->do_action( 'after_field_input', $field, $this ); ?> 989 | 990 | <?php if ( $desc ) : ?> 991 | <p class="description"><?php echo $desc ?></p> 992 | <?php endif ?> 993 | </fieldset> 994 | 995 | <?php 996 | } 997 | 998 | /** 999 | * @since 0.1.0 1000 | * @param array $field 1001 | * @return void 1002 | */ 1003 | protected function render_field_checkbox ( $field ) { 1004 | $id = $field['id']; 1005 | $name = $field['name']; 1006 | $title = $field['title'] ?? $id; 1007 | $name = $field['name']; 1008 | $desc = $field['description']; 1009 | $label = $field['label'] ?? $this->strings['checkbox_enable']; 1010 | $value = (bool) $this->get_field_value( $field ); 1011 | 1012 | $atts = $field['attributes'] ?? []; 1013 | $atts['type'] = 'checkbox'; 1014 | $atts['id'] = $name; 1015 | $atts['name'] = $name; 1016 | $atts['value'] = '1'; 1017 | 1018 | unset( $atts['value'] ); 1019 | unset( $atts['checked'] ); 1020 | ?> 1021 | 1022 | <fieldset> 1023 | <legend class="screen-reader-text"> 1024 | <span><?php echo \esc_html( strip_tags( $title ) ); ?></span> 1025 | </legend> 1026 | <label for="<?php echo \esc_attr( $name ); ?>"> 1027 | <input <?php echo self::parse_tag_atts( $atts ); ?> <?php \checked( $value ); ?>> 1028 | <span class="option-label"><?php echo $label ?></span> 1029 | </label> 1030 | 1031 | <?php $this->do_action( 'after_field_input', $field, $this ); ?> 1032 | 1033 | <?php if ( $desc ) : ?> 1034 | <p class="description"><?php echo $desc ?></p> 1035 | <?php endif ?> 1036 | </fieldset> 1037 | 1038 | <?php 1039 | } 1040 | 1041 | /** 1042 | * @since 0.1.0 1043 | * @param array $field 1044 | * @return void 1045 | */ 1046 | protected function render_field_checkboxes ( $field ) { 1047 | $id = $field['id']; 1048 | $name = $field['name']; 1049 | $title = $field['title'] ?? $id; 1050 | $desc = $field['description']; 1051 | $options = $this->parse_options( $field['options'] ?? [] ); 1052 | $value = $this->get_field_value( $field ); 1053 | $value = is_array( $value ) ? $value : [ $value ]; 1054 | 1055 | $atts = $field['attributes'] ?? []; 1056 | $atts['type'] = 'checkbox'; 1057 | $atts['name'] = $name . '[]'; 1058 | 1059 | unset( $atts['id'] ); 1060 | unset( $atts['value'] ); 1061 | unset( $atts['checked'] ); 1062 | ?> 1063 | 1064 | <fieldset> 1065 | <legend class="screen-reader-text"><span><?php echo \esc_html( strip_tags( $title ) ); ?></span></legend> 1066 | 1067 | <?php foreach ( $options as $opt_value => $opt_label ) : 1068 | $option_id = \esc_attr( $id . '_' . $opt_value ); 1069 | $checked = \in_array( $opt_value, $value ) ? 'checked' : '' ?> 1070 | <label for="<?php echo \esc_attr( $option_id ) ?>"> 1071 | <input <?php echo self::parse_tag_atts( $atts ); ?> id="<?php echo \esc_attr( $option_id ) ?>" value="<?php echo \esc_attr( $opt_value ) ?>" <?php echo $checked ?>> 1072 | <span class="option-label"><?php echo $opt_label ?></span> 1073 | </label> 1074 | <br> 1075 | <?php endforeach ?> 1076 | 1077 | <?php $this->do_action( 'after_field_input', $field, $this ); ?> 1078 | 1079 | <?php if ( $desc ) : ?> 1080 | <p class="description"><?php echo $desc ?></p> 1081 | <?php endif ?> 1082 | 1083 | </fieldset> 1084 | 1085 | <?php 1086 | } 1087 | 1088 | /** 1089 | * @since 0.1.0 1090 | * @param array $field 1091 | * @return void 1092 | */ 1093 | protected function render_field_title ( $field ) { 1094 | $desc = $field['description']; 1095 | $tag = \esc_html( $field['tag'] ?? 'h2' ); 1096 | $atts = $field['attributes'] ?? []; 1097 | 1098 | $title_html = "<{$tag} " . self::parse_tag_atts( $atts ) . '>'; 1099 | $title_html .= $field['title'] ?? ''; 1100 | $title_html .= "</{$tag}>"; 1101 | ?> 1102 | <?php echo $title_html; ?> 1103 | <?php if ( $desc ) : ?> 1104 | <p><?php echo $desc ?></p> 1105 | <?php endif ?> 1106 | <?php 1107 | } 1108 | 1109 | /** 1110 | * @since 0.1.0 1111 | * @param array $field 1112 | * @return void 1113 | */ 1114 | protected function render_field_submit ( $field ) { 1115 | $title = $field['title'] ?? $this->strings['submit_button_label']; 1116 | $atts = $field['attributes'] ?? []; 1117 | 1118 | $atts['type'] = 'submit'; 1119 | $atts['name'] = $atts['name'] ?? 'submit'; 1120 | $atts['id'] = $atts['id'] ?? $atts['name']; 1121 | $atts['class'] = $atts['class'] ?? 'button button-primary'; 1122 | $atts['value'] = $atts['value'] ?? $title; 1123 | 1124 | ?> 1125 | <p class="submit"> 1126 | <?php $this->do_action( 'before_submit_button', $this ) ?> 1127 | 1128 | <button <?php echo self::parse_tag_atts( $atts ); ?>> 1129 | <?php echo \esc_html( $title ); ?> 1130 | </button> 1131 | 1132 | <?php $this->do_action( 'after_submit_button', $this ) ?> 1133 | </p> 1134 | <?php 1135 | } 1136 | 1137 | /** 1138 | * @since 0.2.0 1139 | * @param string $hook_name 1140 | * @param callable|string|array $callback 1141 | * @param integer $priority 1142 | * @param integer $args 1143 | * @return bool 1144 | */ 1145 | public function add_action ( $hook_name, $callback, $priority = 10, $args = 1 ) { 1146 | return \add_filter( $this->hook_prefix . $hook_name, $callback, $priority, $args ); 1147 | } 1148 | 1149 | /** 1150 | * @since 0.2.0 1151 | * @param string $hook_name 1152 | * @param callable|string|array $callback 1153 | * @param integer $priority 1154 | * @param integer $args 1155 | * @return bool 1156 | */ 1157 | public function add_filter ( $hook_name, $callback, $priority = 10, $args = 1 ) { 1158 | return \add_filter( $this->hook_prefix . $hook_name, $callback, $priority, $args ); 1159 | } 1160 | 1161 | /** 1162 | * @since 0.3.0 1163 | * @param string $hook_name 1164 | * @param mixed ...$args 1165 | * @return void 1166 | */ 1167 | public function do_action ( $hook_name, ...$args ) { 1168 | \do_action( $this->hook_prefix . $hook_name, ...$args ); 1169 | } 1170 | 1171 | /** 1172 | * @since 0.3.0 1173 | * @param string $hook_name 1174 | * @param mixed $value 1175 | * @param mixed ...$args 1176 | * @return mixed The filtered value 1177 | */ 1178 | public function apply_filters ( $hook_name, $value, ...$args ) { 1179 | return \apply_filters( $this->hook_prefix . $hook_name, $value, ...$args ); 1180 | } 1181 | 1182 | /** 1183 | * Check if this instance page supports a given feature. 1184 | * 1185 | * @since 0.3.0 1186 | * @param string $feature string The name of a feature to test support for. 1187 | * @return bool True if the gateway supports the feature, false otherwise. 1188 | */ 1189 | public function supports ( $feature ) { 1190 | return isset( $this->supports[ $feature ] ) || \in_array( $feature, $this->supports, true ); 1191 | } 1192 | 1193 | /** 1194 | * @since 0.3.0 1195 | * @param array $atts 1196 | * @return string 1197 | */ 1198 | public static function parse_tag_atts ( $atts ) { 1199 | $list = []; 1200 | foreach ( $atts as $name => $value ) { 1201 | // allow anonymous functions 1202 | if ( is_callable( $value ) && is_object( $value ) ) { 1203 | $value = call_user_func( $value ); 1204 | } 1205 | // don't allow objects or arrays 1206 | elseif ( \is_object( $value ) || is_array( $value ) ) { 1207 | throw new \Exception( "Invalid value at key \"$name\" in " . __METHOD__ . ': array or objects are not allowed.' ); 1208 | } 1209 | 1210 | // delete the attribute with value null/false 1211 | if ( in_array( $value, [ false, null ], true ) ) continue; 1212 | 1213 | // display only the attribute name if has empty string or TRUE 1214 | if ( true === $value ) $value = ''; 1215 | 1216 | $attr = \esc_html( $name ); 1217 | $value = strval( $value ); 1218 | if ( '' !== $value ) { 1219 | $attr .= '="' . \esc_attr( $value ) . '"'; 1220 | } 1221 | $list[] = $attr; 1222 | } 1223 | return \implode( ' ', $list ); 1224 | } 1225 | 1226 | /** 1227 | * @since 0.7.0 1228 | * @param array $options 1229 | * @return array 1230 | */ 1231 | protected function parse_options ( $options ) { 1232 | if ( is_callable( $options ) ) { 1233 | $options = (array) call_user_func( $options ); 1234 | } 1235 | return $options; 1236 | } 1237 | } 1238 | --------------------------------------------------------------------------------