├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── class-wp-osa.php └── wposa-init.php /CHANGELOG.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmadawais/WP-OOP-Settings-API/dcbd9560c3a0613a2409602136dc915bd6a61ec0/CHANGELOG.md -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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 |

2 | 3 | 4 | WordPress OOP Settings API 5 | 6 |

7 | 8 | 9 | [![Tweet for help](https://img.shields.io/twitter/follow/mrahmadawais.svg?style=social&label=Tweet%20@MrAhmadAwais)](https://twitter.com/mrahmadawais/) [![GitHub stars](https://img.shields.io/github/stars/ahmadawais/WP-OOP-Settings-API.svg?style=social&label=Stars)](https://github.com/ahmadawais/WP-OOP-Settings-API/stargazers) [![GitHub followers](https://img.shields.io/github/followers/ahmadawais.svg?style=social&label=Follow)](https://github.com/ahmadawais?tab=followers) — :point_up: Make sure you :star: and :eyes: this repository! 10 | 11 | > Ever wanted to build custom settings inside your WordPress plugin or theme and didn't like the non-DRY approach for creating custom settings via WordPress API? Well, that's why and when I wrote this OOP Wrapper for WordPress Settings API. 🎊 12 | 13 | ![Screenshots](https://on.ahmda.ws/qPBC/c) 14 | 15 | ## Screenshots 16 | 17 | ![](https://i.imgur.com/EXUoeLZ.png) 18 | ![](https://i.imgur.com/sc9816W.png) 19 | ![](https://i.imgur.com/0SWjn4A.png) 20 | 21 | ## TODO: 22 | - [x] Basic Settings Page 23 | - [x] Tabs on Settings Page with JS 24 | - [x] Tabs on Settings Page with JS 25 | - [x] Documentation for code workflow 26 | - [x] Create Field: `text` 27 | - [x] Create Field: `textarea` 28 | - [x] Create Field: `url` 29 | - [x] Create Field: `number` 30 | - [x] Create Field: `checkbox` 31 | - [x] Create Field: `multicheck` 32 | - [x] Create Field: `radio` 33 | - [x] Create Field: `select` 34 | - [x] Create Field: `html` 35 | - [x] Create Field: `wysiwyg` 36 | - [x] Create Field: `file` 37 | - [x] Create Field: `image` 38 | - [x] Create Field: `password` 39 | - [x] Create Field: `color` 40 | - [ ] Tutorials 41 | - [ ] Blog post 42 | - [ ] Documentation 43 | - [ ] Re-factor the code with WP Standards 44 | - [ ] Re-factor the code into classes 45 | 46 | ![License](https://on.ahmda.ws/qNys/c) 47 | 48 | ## License 49 | Release under GNU GPL v2.0 50 | 51 | 52 | ![Credits](https://on.ahmda.ws/qOxs/c) 53 | 54 | ## Credits 55 | 56 | @AhmadAwais, @deviorobert, @MaedahBatool 57 | AND @WordPress, @tareq1988, @royboy789, @twigpress. 58 | 59 | 60 | --- 61 | ![Hello](https://on.ahmda.ws/3dea3a3b1de3/c) 62 | 63 | ### 🙌 [THEDEVCOUPLE PARTNERS](https://TheDevCouple.com/partners) 64 | 65 | This open source project is maintained by the help of awesome businesses listed below. What? [Read more about it →](https://TheDevCouple.com/partners) 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 |
77 | 78 |
79 |
80 |

81 | For anything else, tweet at @MrAhmadAwais 82 |

83 | 84 |
85 |

I have released a video course to help you become a better developer — Become a VSCode Power User →

86 |
87 | 88 |
VSCode
89 | 90 | _VSCode Power User Course →_ 91 |
92 | -------------------------------------------------------------------------------- /class-wp-osa.php: -------------------------------------------------------------------------------- 1 | sections_array = $sections; 98 | 99 | return $this; 100 | } 101 | 102 | 103 | /** 104 | * Add a single section. 105 | * 106 | * @param array $section 107 | * @since 1.0.0 108 | */ 109 | public function add_section( $section ) { 110 | // Bail if not array. 111 | if ( ! is_array( $section ) ) { 112 | return false; 113 | } 114 | 115 | // Assign the section to sections array. 116 | $this->sections_array[] = $section; 117 | 118 | return $this; 119 | } 120 | 121 | 122 | /** 123 | * Set Fields. 124 | * 125 | * @since 1.0.0 126 | */ 127 | public function set_fields( $fields ) { 128 | // Bail if not array. 129 | if ( ! is_array( $fields ) ) { 130 | return false; 131 | } 132 | 133 | // Assign the fields. 134 | $this->fields_array = $fields; 135 | 136 | return $this; 137 | } 138 | 139 | 140 | 141 | /** 142 | * Add a single field. 143 | * 144 | * @since 1.0.0 145 | */ 146 | public function add_field( $section, $field_array ) { 147 | // Set the defaults 148 | $defaults = array( 149 | 'id' => '', 150 | 'name' => '', 151 | 'desc' => '', 152 | 'type' => 'text', 153 | ); 154 | 155 | // Combine the defaults with user's arguements. 156 | $arg = wp_parse_args( $field_array, $defaults ); 157 | 158 | // Each field is an array named against its section. 159 | $this->fields_array[ $section ][] = $arg; 160 | 161 | return $this; 162 | } 163 | 164 | 165 | 166 | /** 167 | * Initialize API. 168 | * 169 | * Initializes and registers the settings sections and fields. 170 | * Usually this should be called at `admin_init` hook. 171 | * 172 | * @since 1.0.0 173 | */ 174 | function admin_init() { 175 | /** 176 | * Register the sections. 177 | * 178 | * Sections array is like this: 179 | * 180 | * $sections_array = array ( 181 | * $section_array, 182 | * $section_array, 183 | * $section_array, 184 | * ); 185 | * 186 | * Section array is like this: 187 | * 188 | * $section_array = array ( 189 | * 'id' => 'section_id', 190 | * 'title' => 'Section Title' 191 | * ); 192 | * 193 | * @since 1.0.0 194 | */ 195 | foreach ( $this->sections_array as $section ) { 196 | if ( false == get_option( $section['id'] ) ) { 197 | // Add a new field as section ID. 198 | add_option( $section['id'] ); 199 | } 200 | 201 | // Deals with sections description. 202 | if ( isset( $section['desc'] ) && ! empty( $section['desc'] ) ) { 203 | // Build HTML. 204 | $section['desc'] = '
' . $section['desc'] . '
'; 205 | 206 | // Create the callback for description. 207 | $callback = function() use ( $section ) { 208 | echo str_replace( '"', '\"', $section['desc'] ); 209 | }; 210 | 211 | } elseif ( isset( $section['callback'] ) ) { 212 | $callback = $section['callback']; 213 | } else { 214 | $callback = null; 215 | } 216 | 217 | /** 218 | * Add a new section to a settings page. 219 | * 220 | * @param string $id 221 | * @param string $title 222 | * @param callable $callback 223 | * @param string $page | Page is same as section ID. 224 | * @since 1.0.0 225 | */ 226 | add_settings_section( $section['id'], $section['title'], $callback, $section['id'] ); 227 | } // foreach ended. 228 | 229 | /** 230 | * Register settings fields. 231 | * 232 | * Fields array is like this: 233 | * 234 | * $fields_array = array ( 235 | * $section => $field_array, 236 | * $section => $field_array, 237 | * $section => $field_array, 238 | * ); 239 | * 240 | * 241 | * Field array is like this: 242 | * 243 | * $field_array = array ( 244 | * 'id' => 'id', 245 | * 'name' => 'Name', 246 | * 'type' => 'text', 247 | * ); 248 | * 249 | * @since 1.0.0 250 | */ 251 | foreach ( $this->fields_array as $section => $field_array ) { 252 | foreach ( $field_array as $field ) { 253 | // ID. 254 | $id = isset( $field['id'] ) ? $field['id'] : false; 255 | 256 | // Type. 257 | $type = isset( $field['type'] ) ? $field['type'] : 'text'; 258 | 259 | // Name. 260 | $name = isset( $field['name'] ) ? $field['name'] : 'No Name Added'; 261 | 262 | // Label for. 263 | $label_for = "{$section}[{$field['id']}]"; 264 | 265 | // Description. 266 | $description = isset( $field['desc'] ) ? $field['desc'] : ''; 267 | 268 | // Size. 269 | $size = isset( $field['size'] ) ? $field['size'] : null; 270 | 271 | // Options. 272 | $options = isset( $field['options'] ) ? $field['options'] : ''; 273 | 274 | // Standard default value. 275 | $default = isset( $field['default'] ) ? $field['default'] : ''; 276 | 277 | // Standard default placeholder. 278 | $placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; 279 | 280 | // Sanitize Callback. 281 | $sanitize_callback = isset( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : ''; 282 | 283 | $args = array( 284 | 'id' => $id, 285 | 'type' => $type, 286 | 'name' => $name, 287 | 'label_for' => $label_for, 288 | 'desc' => $description, 289 | 'section' => $section, 290 | 'size' => $size, 291 | 'options' => $options, 292 | 'std' => $default, 293 | 'placeholder' => $placeholder, 294 | 'sanitize_callback' => $sanitize_callback, 295 | ); 296 | 297 | /** 298 | * Add a new field to a section of a settings page. 299 | * 300 | * @param string $id 301 | * @param string $title 302 | * @param callable $callback 303 | * @param string $page 304 | * @param string $section = 'default' 305 | * @param array $args = array() 306 | * @since 1.0.0 307 | */ 308 | 309 | // @param string $id 310 | $field_id = $section . '[' . $field['id'] . ']'; 311 | 312 | add_settings_field( 313 | $field_id, 314 | $name, 315 | array( $this, 'callback_' . $type ), 316 | $section, 317 | $section, 318 | $args 319 | ); 320 | } // foreach ended. 321 | } // foreach ended. 322 | 323 | // Creates our settings in the fields table. 324 | foreach ( $this->sections_array as $section ) { 325 | /** 326 | * Registers a setting and its sanitization callback. 327 | * 328 | * @param string $field_group | A settings group name. 329 | * @param string $field_name | The name of an option to sanitize and save. 330 | * @param callable $sanitize_callback = '' 331 | * @since 1.0.0 332 | */ 333 | register_setting( $section['id'], $section['id'], array( $this, 'sanitize_fields' ) ); 334 | } // foreach ended. 335 | 336 | } // admin_init() ended. 337 | 338 | 339 | /** 340 | * Sanitize callback for Settings API fields. 341 | * 342 | * @since 1.0.0 343 | */ 344 | public function sanitize_fields( $fields ) { 345 | foreach ( $fields as $field_slug => $field_value ) { 346 | $sanitize_callback = $this->get_sanitize_callback( $field_slug ); 347 | 348 | // If callback is set, call it. 349 | if ( $sanitize_callback ) { 350 | $fields[ $field_slug ] = call_user_func( $sanitize_callback, $field_value ); 351 | continue; 352 | } 353 | } 354 | 355 | return $fields; 356 | } 357 | 358 | 359 | /** 360 | * Get sanitization callback for given option slug 361 | * 362 | * @param string $slug option slug. 363 | * @return mixed string | bool false 364 | * @since 1.0.0 365 | */ 366 | function get_sanitize_callback( $slug = '' ) { 367 | if ( empty( $slug ) ) { 368 | return false; 369 | } 370 | 371 | // Iterate over registered fields and see if we can find proper callback. 372 | foreach ( $this->fields_array as $section => $field_array ) { 373 | foreach ( $field_array as $field ) { 374 | if ( $field['name'] != $slug ) { 375 | continue; 376 | } 377 | 378 | // Return the callback name. 379 | return isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : false; 380 | } 381 | } 382 | 383 | return false; 384 | } 385 | 386 | 387 | /** 388 | * Get field description for display 389 | * 390 | * @param array $args settings field args 391 | */ 392 | public function get_field_description( $args ) { 393 | if ( ! empty( $args['desc'] ) ) { 394 | $desc = sprintf( '

%s

', $args['desc'] ); 395 | } else { 396 | $desc = ''; 397 | } 398 | 399 | return $desc; 400 | } 401 | 402 | 403 | /** 404 | * Displays a title field for a settings field 405 | * 406 | * @param array $args settings field args 407 | */ 408 | function callback_title( $args ) { 409 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 410 | if ( '' !== $args['name'] ) { 411 | $name = $args['name']; 412 | } else { 413 | }; 414 | $type = isset( $args['type'] ) ? $args['type'] : 'title'; 415 | 416 | $html = ''; 417 | echo $html; 418 | } 419 | 420 | 421 | /** 422 | * Displays a text field for a settings field 423 | * 424 | * @param array $args settings field args 425 | */ 426 | function callback_text( $args ) { 427 | 428 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'], $args['placeholder'] ) ); 429 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; 430 | $type = isset( $args['type'] ) ? $args['type'] : 'text'; 431 | 432 | $html = sprintf( '', $type, $size, $args['section'], $args['id'], $value, $args['placeholder'] ); 433 | $html .= $this->get_field_description( $args ); 434 | 435 | echo $html; 436 | } 437 | 438 | 439 | /** 440 | * Displays a url field for a settings field 441 | * 442 | * @param array $args settings field args 443 | */ 444 | function callback_url( $args ) { 445 | $this->callback_text( $args ); 446 | } 447 | 448 | /** 449 | * Displays a number field for a settings field 450 | * 451 | * @param array $args settings field args 452 | */ 453 | function callback_number( $args ) { 454 | $this->callback_text( $args ); 455 | } 456 | 457 | /** 458 | * Displays a checkbox for a settings field 459 | * 460 | * @param array $args settings field args 461 | */ 462 | function callback_checkbox( $args ) { 463 | 464 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 465 | 466 | $html = '
'; 467 | $html .= sprintf( '', $args['desc'] ); 471 | $html .= '
'; 472 | 473 | echo $html; 474 | } 475 | 476 | /** 477 | * Displays a multicheckbox a settings field 478 | * 479 | * @param array $args settings field args 480 | */ 481 | function callback_multicheck( $args ) { 482 | 483 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); 484 | 485 | $html = '
'; 486 | foreach ( $args['options'] as $key => $label ) { 487 | $checked = isset( $value[ $key ] ) ? $value[ $key ] : '0'; 488 | $html .= sprintf( '
', $label ); 491 | } 492 | $html .= $this->get_field_description( $args ); 493 | $html .= '
'; 494 | 495 | echo $html; 496 | } 497 | 498 | /** 499 | * Displays a multicheckbox a settings field 500 | * 501 | * @param array $args settings field args 502 | */ 503 | function callback_radio( $args ) { 504 | 505 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); 506 | 507 | $html = '
'; 508 | foreach ( $args['options'] as $key => $label ) { 509 | $html .= sprintf( '
', $label ); 512 | } 513 | $html .= $this->get_field_description( $args ); 514 | $html .= '
'; 515 | 516 | echo $html; 517 | } 518 | 519 | /** 520 | * Displays a selectbox for a settings field 521 | * 522 | * @param array $args settings field args 523 | */ 524 | function callback_select( $args ) { 525 | 526 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 527 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; 528 | 529 | $html = sprintf( '' ); 534 | $html .= $this->get_field_description( $args ); 535 | 536 | echo $html; 537 | } 538 | 539 | /** 540 | * Displays a textarea for a settings field 541 | * 542 | * @param array $args settings field args 543 | */ 544 | function callback_textarea( $args ) { 545 | 546 | $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 547 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; 548 | 549 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 550 | $html .= $this->get_field_description( $args ); 551 | 552 | echo $html; 553 | } 554 | 555 | /** 556 | * Displays a textarea for a settings field 557 | * 558 | * @param array $args settings field args. 559 | * @return string 560 | */ 561 | function callback_html( $args ) { 562 | echo $this->get_field_description( $args ); 563 | } 564 | 565 | /** 566 | * Displays a rich text textarea for a settings field 567 | * 568 | * @param array $args settings field args. 569 | */ 570 | function callback_wysiwyg( $args ) { 571 | 572 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); 573 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : '500px'; 574 | 575 | echo '
'; 576 | 577 | $editor_settings = array( 578 | 'teeny' => true, 579 | 'textarea_name' => $args['section'] . '[' . $args['id'] . ']', 580 | 'textarea_rows' => 10, 581 | ); 582 | if ( isset( $args['options'] ) && is_array( $args['options'] ) ) { 583 | $editor_settings = array_merge( $editor_settings, $args['options'] ); 584 | } 585 | 586 | wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings ); 587 | 588 | echo '
'; 589 | 590 | echo $this->get_field_description( $args ); 591 | } 592 | 593 | /** 594 | * Displays a file upload field for a settings field 595 | * 596 | * @param array $args settings field args. 597 | */ 598 | function callback_file( $args ) { 599 | 600 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 601 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; 602 | $id = $args['section'] . '[' . $args['id'] . ']'; 603 | $label = isset( $args['options']['button_label'] ) ? 604 | $args['options']['button_label'] : 605 | __( 'Choose File' ); 606 | 607 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 608 | $html .= ''; 609 | $html .= $this->get_field_description( $args ); 610 | 611 | echo $html; 612 | } 613 | 614 | /** 615 | * Displays an image upload field with a preview 616 | * 617 | * @param array $args settings field args. 618 | */ 619 | function callback_image( $args ) { 620 | 621 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 622 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; 623 | $id = $args['section'] . '[' . $args['id'] . ']'; 624 | $label = isset( $args['options']['button_label'] ) ? 625 | $args['options']['button_label'] : 626 | __( 'Choose Image' ); 627 | 628 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 629 | $html .= ''; 630 | $html .= $this->get_field_description( $args ); 631 | $html .= '

'; 632 | 633 | echo $html; 634 | } 635 | 636 | /** 637 | * Displays a password field for a settings field 638 | * 639 | * @param array $args settings field args 640 | */ 641 | function callback_password( $args ) { 642 | 643 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); 644 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; 645 | 646 | $html = sprintf( '', $size, $args['section'], $args['id'], $value ); 647 | $html .= $this->get_field_description( $args ); 648 | 649 | echo $html; 650 | } 651 | 652 | /** 653 | * Displays a color picker field for a settings field 654 | * 655 | * @param array $args settings field args 656 | */ 657 | function callback_color( $args ) { 658 | 659 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'], $args['placeholder'] ) ); 660 | $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; 661 | 662 | $html = sprintf( '', $size, $args['section'], $args['id'], $value, $args['std'], $args['placeholder'] ); 663 | $html .= $this->get_field_description( $args ); 664 | 665 | echo $html; 666 | } 667 | 668 | 669 | /** 670 | * Displays a separator field for a settings field 671 | * 672 | * @param array $args settings field args 673 | */ 674 | function callback_separator( $args ) { 675 | $type = isset( $args['type'] ) ? $args['type'] : 'separator'; 676 | 677 | $html = ''; 678 | $html .= '
'; 679 | echo $html; 680 | } 681 | 682 | 683 | /** 684 | * Get the value of a settings field 685 | * 686 | * @param string $option settings field name. 687 | * @param string $section the section name this field belongs to. 688 | * @param string $default default text if it's not found. 689 | * @return string 690 | */ 691 | function get_option( $option, $section, $default = '' ) { 692 | 693 | $options = get_option( $section ); 694 | 695 | if ( isset( $options[ $option ] ) ) { 696 | return $options[ $option ]; 697 | } 698 | 699 | return $default; 700 | } 701 | 702 | /** 703 | * Add submenu page to the Settings main menu. 704 | * 705 | * @param string $page_title 706 | * @param string $menu_title 707 | * @param string $capability 708 | * @param string $menu_slug 709 | * @param callable $function = '' 710 | * @author Ahmad Awais 711 | * @since [version] 712 | */ 713 | 714 | // public function admin_menu( $page_title = 'Page Title', $menu_title = 'Menu Title', $capability = 'manage_options', $menu_slug = 'settings_page', $callable = 'plugin_page' ) { 715 | public function admin_menu() { 716 | // add_options_page( $page_title, $menu_title, $capability, $menu_slug, array( $this, $callable ) ); 717 | add_options_page( 718 | 'WP OOP Settings API', 719 | 'WP OOP Settings API', 720 | 'manage_options', 721 | 'wp_osa_settings', 722 | array( $this, 'plugin_page' ) 723 | ); 724 | } 725 | 726 | public function plugin_page() { 727 | echo '
'; 728 | echo '

WP OOP Settings API v' . WPOSA_VERSION . '

'; 729 | $this->show_navigation(); 730 | $this->show_forms(); 731 | echo '
'; 732 | } 733 | 734 | /** 735 | * Show navigations as tab 736 | * 737 | * Shows all the settings section labels as tab 738 | */ 739 | function show_navigation() { 740 | $html = ''; 747 | 748 | echo $html; 749 | } 750 | 751 | /** 752 | * Show the section settings forms 753 | * 754 | * This function displays every sections in a different form 755 | */ 756 | function show_forms() { 757 | ?> 758 |
759 | sections_array as $form ) { ?> 760 | 761 |
762 |
763 | 769 |
770 | 771 |
772 |
773 |
774 | 775 |
776 | script(); 778 | } 779 | 780 | /** 781 | * Tabbable JavaScript codes & Initiate Color Picker 782 | * 783 | * This code uses localstorage for displaying active tabs 784 | */ 785 | function script() { 786 | ?> 787 | 886 | 887 | 915 | add_section( 77 | array( 78 | 'id' => 'wposa_basic', 79 | 'title' => __( 'Basic Settings', 'WPOSA' ), 80 | ) 81 | ); 82 | 83 | // Section: Other Settings. 84 | $wposa_obj->add_section( 85 | array( 86 | 'id' => 'wposa_other', 87 | 'title' => __( 'Other Settings', 'WPOSA' ), 88 | ) 89 | ); 90 | 91 | // Field: Text. 92 | $wposa_obj->add_field( 93 | 'wposa_basic', 94 | array( 95 | 'id' => 'text', 96 | 'type' => 'text', 97 | 'name' => __( 'Text Input', 'WPOSA' ), 98 | 'desc' => __( 'Text input description', 'WPOSA' ), 99 | 'default' => 'Default Text', 100 | ) 101 | ); 102 | 103 | // Field: Number. 104 | $wposa_obj->add_field( 105 | 'wposa_basic', 106 | array( 107 | 'id' => 'text_no', 108 | 'type' => 'number', 109 | 'name' => __( 'Number Input', 'WPOSA' ), 110 | 'desc' => __( 'Number field with validation callback `intval`', 'WPOSA' ), 111 | 'default' => 1, 112 | 'sanitize_callback' => 'intval', 113 | ) 114 | ); 115 | 116 | // Field: Password. 117 | $wposa_obj->add_field( 118 | 'wposa_basic', 119 | array( 120 | 'id' => 'password', 121 | 'type' => 'password', 122 | 'name' => __( 'Password Input', 'WPOSA' ), 123 | 'desc' => __( 'Password field description', 'WPOSA' ), 124 | ) 125 | ); 126 | 127 | // Field: Textarea. 128 | $wposa_obj->add_field( 129 | 'wposa_basic', 130 | array( 131 | 'id' => 'textarea', 132 | 'type' => 'textarea', 133 | 'name' => __( 'Textarea Input', 'WPOSA' ), 134 | 'desc' => __( 'Textarea description', 'WPOSA' ), 135 | ) 136 | ); 137 | 138 | // Field: Separator. 139 | $wposa_obj->add_field( 140 | 'wposa_basic', 141 | array( 142 | 'id' => 'separator', 143 | 'type' => 'separator', 144 | ) 145 | ); 146 | 147 | // Field: Title. 148 | $wposa_obj->add_field( 149 | 'wposa_basic', 150 | array( 151 | 'id' => 'title', 152 | 'type' => 'title', 153 | 'name' => '

Title

', 154 | ) 155 | ); 156 | 157 | // Field: Checkbox. 158 | $wposa_obj->add_field( 159 | 'wposa_basic', 160 | array( 161 | 'id' => 'checkbox', 162 | 'type' => 'checkbox', 163 | 'name' => __( 'Checkbox', 'WPOSA' ), 164 | 'desc' => __( 'Checkbox Label', 'WPOSA' ), 165 | ) 166 | ); 167 | 168 | // Field: Radio. 169 | $wposa_obj->add_field( 170 | 'wposa_basic', 171 | array( 172 | 'id' => 'radio', 173 | 'type' => 'radio', 174 | 'name' => __( 'Radio', 'WPOSA' ), 175 | 'desc' => __( 'Radio Button', 'WPOSA' ), 176 | 'options' => array( 177 | 'yes' => 'Yes', 178 | 'no' => 'No', 179 | ), 180 | ) 181 | ); 182 | 183 | // Field: Multicheck. 184 | $wposa_obj->add_field( 185 | 'wposa_basic', 186 | array( 187 | 'id' => 'multicheck', 188 | 'type' => 'multicheck', 189 | 'name' => __( 'Multile checkbox', 'WPOSA' ), 190 | 'desc' => __( 'Multile checkbox description', 'WPOSA' ), 191 | 'options' => array( 192 | 'yes' => 'Yes', 193 | 'no' => 'No', 194 | ), 195 | ) 196 | ); 197 | 198 | // Field: Select. 199 | $wposa_obj->add_field( 200 | 'wposa_basic', 201 | array( 202 | 'id' => 'select', 203 | 'type' => 'select', 204 | 'name' => __( 'A Dropdown', 'WPOSA' ), 205 | 'desc' => __( 'A Dropdown description', 'WPOSA' ), 206 | 'options' => array( 207 | 'yes' => 'Yes', 208 | 'no' => 'No', 209 | ), 210 | ) 211 | ); 212 | 213 | // Field: Image. 214 | $wposa_obj->add_field( 215 | 'wposa_other', 216 | array( 217 | 'id' => 'image', 218 | 'type' => 'image', 219 | 'name' => __( 'Image', 'WPOSA' ), 220 | 'desc' => __( 'Image description', 'WPOSA' ), 221 | 'options' => array( 222 | 'button_label' => 'Choose Image', 223 | ), 224 | ) 225 | ); 226 | 227 | // Field: File. 228 | $wposa_obj->add_field( 229 | 'wposa_other', 230 | array( 231 | 'id' => 'file', 232 | 'type' => 'file', 233 | 'name' => __( 'File', 'WPOSA' ), 234 | 'desc' => __( 'File description', 'WPOSA' ), 235 | 'options' => array( 236 | 'button_label' => 'Choose file', 237 | ), 238 | ) 239 | ); 240 | 241 | // Field: Color. 242 | $wposa_obj->add_field( 243 | 'wposa_other', 244 | array( 245 | 'id' => 'color', 246 | 'type' => 'color', 247 | 'name' => __( 'Color', 'WPOSA' ), 248 | 'desc' => __( 'Color description', 'WPOSA' ), 249 | 'placeholder' => __( '#5F4B8B', 'WPOSA' ), 250 | ) 251 | ); 252 | 253 | // Field: WYSIWYG. 254 | $wposa_obj->add_field( 255 | 'wposa_other', 256 | array( 257 | 'id' => 'wysiwyg', 258 | 'type' => 'wysiwyg', 259 | 'name' => __( 'WP_Editor', 'WPOSA' ), 260 | 'desc' => __( 'WP_Editor description', 'WPOSA' ), 261 | ) 262 | ); 263 | } 264 | --------------------------------------------------------------------------------