├── .editorconfig ├── .gitattributes ├── .gitignore ├── LICENSE ├── assets ├── css │ └── wp-consent-api.css └── js │ ├── wp-consent-api.js │ └── wp-consent-api.min.js ├── composer.json ├── inc ├── api-functions.php ├── class-wp-consent-api-config.php ├── class-wp-consent-api-cookie-info.php ├── class-wp-consent-api-site-health.php └── wordpress-comments-functions.php ├── package.json ├── phpcs.xml ├── phpstan.neon.dist ├── readme.md ├── readme.txt ├── tests └── phpstan │ └── bootstrap.php └── wp-consent-api.php /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | # WordPress Coding Standards 5 | # https://make.wordpress.org/core/handbook/coding-standards/ 6 | 7 | root = true 8 | 9 | [*] 10 | charset = utf-8 11 | end_of_line = lf 12 | insert_final_newline = true 13 | trim_trailing_whitespace = true 14 | indent_style = tab 15 | 16 | [*.yml] 17 | indent_style = space 18 | indent_size = 2 19 | 20 | [*.md] 21 | trim_trailing_whitespace = false 22 | 23 | [{*.txt,wp-config-sample.php}] 24 | end_of_line = crlf 25 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Atom/Serenata configuration 2 | .serenata 3 | 4 | # Composer 5 | vendor/ 6 | composer.lock 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /assets/css/wp-consent-api.css: -------------------------------------------------------------------------------- 1 | 2 | #health-check-accordion-block-wp-consent-api .title{ 3 | pointer-events: none; 4 | font-weight: 600; 5 | flex-grow: 1; 6 | padding-top: 10px; 7 | } 8 | #health-check-accordion-block-wp-consent-api .actions{ 9 | padding: 20px 0px; 10 | } 11 | #health-check-accordion-block-wp-consent-api p{ 12 | 13 | background: #fff; 14 | border-left: 4px solid #fff; 15 | box-shadow: 0 1px 1px 0 rgba(0,0,0,.1); 16 | margin: 5px 15px 2px; 17 | padding: 1px 12px; 18 | border-left-color: #ffb900; 19 | margin: 5px 20px 15px 0px; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /assets/js/wp-consent-api.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /** 3 | * Set consent_type as passed by localize_script, and trigger a hook so geo ip scripts can alter it as needed. 4 | * It's set on document ready, so the consent management plugin can first add an event listener. 5 | * 6 | * 7 | * Edit: chronologically it seems difficult to create a sort of filter for the consent type. 8 | * Let's change it so cookiebanners are just required to set it, it it's not available, we use a default, as defined here. 9 | * 10 | * This way, if a consent management plugin does not define a consenttype, the one passed here will be used, and it will still work. 11 | * 12 | * 13 | */ 14 | 15 | window.wp_fallback_consent_type = consent_api.consent_type; 16 | window.waitfor_consent_hook = consent_api.waitfor_consent_hook; 17 | 18 | /** 19 | * Check if a user has given consent for a specific category. 20 | * 21 | * @param {string} category The category to check consent against. 22 | */ 23 | function wp_has_consent(category) { 24 | var consent_type; 25 | if (typeof (window.wp_consent_type) !== "undefined"){ 26 | consent_type = window.wp_consent_type; 27 | } else { 28 | consent_type = window.wp_fallback_consent_type 29 | } 30 | 31 | var has_consent_level = false; 32 | var cookie_value = consent_api_get_cookie(consent_api.cookie_prefix + '_' + category); 33 | 34 | if (!consent_type) { 35 | //if consent_type is not set, there's no consent management, we should return true to activate all cookies 36 | has_consent_level = true; 37 | 38 | } else if (consent_type.indexOf('optout') !== -1 && cookie_value === '') { 39 | //if it's opt out and no cookie is set we should also return true 40 | has_consent_level = true; 41 | 42 | } else { 43 | //all other situations, return only true if value is allow 44 | has_consent_level = (cookie_value === 'allow'); 45 | } 46 | 47 | return has_consent_level; 48 | } 49 | 50 | /** 51 | * Set cookie by consent type. 52 | * 53 | * @param {string} name The cookie name to set. 54 | * @param {string} value The cookie value to set. 55 | */ 56 | function consent_api_set_cookie(name, value) { 57 | var secure = ";secure"; 58 | var days = consent_api.cookie_expiration; 59 | var date = new Date(); 60 | date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 61 | var expires = ";expires=" + date.toGMTString(); 62 | 63 | if (window.location.protocol !== "https:") secure = ''; 64 | 65 | document.cookie = name + "=" + value + secure + expires + ";path=/"; 66 | } 67 | 68 | /** 69 | * Retrieve a cookie by name. 70 | * 71 | * @param {string} name The name of the cookie to get data from. 72 | */ 73 | function consent_api_get_cookie(name) { 74 | name = name + "="; 75 | var cookies = window.document.cookie.split(';'); 76 | 77 | for (var i = 0; i < cookies.length; i++) { 78 | var cookie = cookies[i].trim(); 79 | if (cookie.indexOf(name) == 0) 80 | return cookie.substring(name.length, cookie.length); 81 | } 82 | 83 | //If we get to this point, that means the cookie wasn't found, we return an empty string. 84 | return ""; 85 | } 86 | 87 | /** 88 | * Set a new consent category value. 89 | * 90 | * @param {string} category The consent category to update. 91 | * @param {string} value The value to update the consent category to. 92 | */ 93 | function wp_set_consent(category, value) { 94 | var event; 95 | if (value !== 'allow' && value !== 'deny') return; 96 | var previous_value = consent_api_get_cookie(consent_api.cookie_prefix + '_' + category); 97 | consent_api_set_cookie(consent_api.cookie_prefix + '_' + category, value); 98 | 99 | //do not trigger a change event if nothing has changed. 100 | if ( previous_value === value ) return; 101 | 102 | var changedConsentCategory = []; 103 | changedConsentCategory[category] = value; 104 | try { 105 | // For modern browsers except IE: 106 | event = new CustomEvent('wp_listen_for_consent_change', {detail: changedConsentCategory}); 107 | } catch (err) { 108 | // If IE 11 (or 10 or 9...?) 109 | event = document.createEvent('Event'); 110 | event.initEvent('wp_listen_for_consent_change', true, true); 111 | event.detail = changedConsentCategory; 112 | } 113 | // Dispatch/Trigger/Fire the event 114 | document.dispatchEvent(event); 115 | } 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /assets/js/wp-consent-api.min.js: -------------------------------------------------------------------------------- 1 | "use strict";window.wp_fallback_consent_type=consent_api.consent_type;window.waitfor_consent_hook=consent_api.waitfor_consent_hook;function wp_has_consent(category){var consent_type;if(typeof window.wp_consent_type!=="undefined"){consent_type=window.wp_consent_type}else{consent_type=window.wp_fallback_consent_type}var has_consent_level=false;var cookie_value=consent_api_get_cookie(consent_api.cookie_prefix+"_"+category);if(!consent_type){has_consent_level=true}else if(consent_type.indexOf("optout")!==-1&&cookie_value===""){has_consent_level=true}else{has_consent_level=cookie_value==="allow"}return has_consent_level}function consent_api_set_cookie(name,value){var secure=";secure";var days=consent_api.cookie_expiration;var date=new Date;date.setTime(date.getTime()+days*24*60*60*1e3);var expires=";expires="+date.toGMTString();if(window.location.protocol!=="https:")secure="";document.cookie=name+"="+value+secure+expires+";path=/"}function consent_api_get_cookie(name){name=name+"=";var cookies=window.document.cookie.split(";");for(var i=0;i=5.6.0" 8 | }, 9 | "require-dev": { 10 | "squizlabs/php_codesniffer": "^3.0", 11 | "wp-coding-standards/wpcs": "^2", 12 | "phpcompatibility/phpcompatibility-wp": "^2.0", 13 | "dealerdirect/phpcodesniffer-composer-installer": "^1.0", 14 | "szepeviktor/phpstan-wordpress": "^2.0" 15 | }, 16 | 17 | "minimum-stability": "dev", 18 | "prefer-stable": true, 19 | 20 | "scripts": { 21 | "phpcs": [ 22 | "phpcs -p -s *.php" 23 | ] 24 | }, 25 | "config": { 26 | "allow-plugins": { 27 | "dealerdirect/phpcodesniffer-composer-installer": true 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /inc/api-functions.php: -------------------------------------------------------------------------------- 1 | consent_cookie_prefix(); 57 | 58 | wp_localize_script( 59 | 'wp-consent-api', 60 | 'consent_api', 61 | array( 62 | 'consent_type' => $consent_type, 63 | 'waitfor_consent_hook' => $waitfor_consent_hook, 64 | 'cookie_expiration' => $expiration, 65 | 'cookie_prefix' => $prefix, 66 | ) 67 | ); 68 | } 69 | add_action( 'wp_enqueue_scripts', 'wp_consent_api_enqueue_assets', PHP_INT_MAX - 100 ); 70 | 71 | /** 72 | * Enqueue style for back-end to show wp-consent-api unregister plugins list in a better style in site health check page. 73 | * 74 | * @param string $hook Hook name. 75 | */ 76 | function wp_consent_api_enqueue_admin_assets( $hook ) { 77 | 78 | if ( 'site-health.php' !== $hook ) { 79 | return; 80 | } 81 | wp_enqueue_style( 'wp-consent-api-css', WP_CONSENT_API_URL . 'assets/css/wp-consent-api.css', array(), WP_CONSENT_API_VERSION ); 82 | } 83 | add_action( 'admin_enqueue_scripts', 'wp_consent_api_enqueue_admin_assets' ); 84 | 85 | /** 86 | * Validates consent type. 87 | * 88 | * @since 1.0.0 89 | * 90 | * @param string $consent_type A consent type. 91 | * 92 | * @return bool|string The validated consent type, or `false`. 93 | */ 94 | function wp_validate_consent_type( $consent_type ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 95 | if ( in_array( $consent_type, WP_Consent_API::$config->consent_types(), true ) ) { 96 | return $consent_type; 97 | } 98 | 99 | return false; 100 | } 101 | 102 | /** 103 | * Validates consent value. 104 | * 105 | * @since 1.0.0 106 | * 107 | * @param string $value A consent value. 108 | * 109 | * @return bool|string The validated consent type, or false. 110 | */ 111 | function wp_validate_consent_value( $value ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 112 | if ( in_array( $value, WP_Consent_API::$config->consent_values(), true ) ) { 113 | return $value; 114 | } 115 | return false; 116 | } 117 | 118 | /** 119 | * Validates consent category. 120 | * 121 | * @since 1.0.0 122 | * 123 | * @param string $category A consent category. 124 | * 125 | * @return bool|string The validated category, or false. 126 | */ 127 | function wp_validate_consent_category( $category ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 128 | if ( in_array( $category, WP_Consent_API::$config->consent_categories(), true ) ) { 129 | return $category; 130 | } 131 | 132 | return false; 133 | } 134 | 135 | /** 136 | * Retrieves active consent type. 137 | * 138 | * @since 1.0.0 139 | * 140 | * @return string|bool 141 | */ 142 | function wp_get_consent_type() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 143 | return apply_filters( 'wp_get_consent_type', false ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- This is intended for Core. 144 | } 145 | 146 | 147 | /** 148 | * Filterable, to allow for use in combination with consent_type 149 | * return value of wp_consent$level cookie (false, deny or allow) 150 | * 151 | * @since 1.0.0 152 | * 153 | * @param string $category The consent category. 154 | * @param string|bool $requested_by Plugin name e.g. complianz-gdpr/complianz-gdpr.php. This can be used to disable consent for a plugin specifically. 155 | * 156 | * @return bool 157 | */ 158 | function wp_has_consent( $category, $requested_by = false ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 159 | $consent_type = wp_get_consent_type(); 160 | $category = wp_validate_consent_category( $category ); 161 | $prefix = WP_Consent_API::$config->consent_cookie_prefix(); 162 | $cookie_name = "{$prefix}_{$category}"; 163 | 164 | if ( ! $consent_type ) { 165 | // If consent_type is not set, there's no consent management, we should 166 | // return true to activate all cookies. 167 | $has_consent = true; 168 | } elseif ( strpos( $consent_type, 'optout' ) !== false && ( ! isset( $_COOKIE[ $cookie_name ] ) ) ) { 169 | // If it's opt out and no cookie is set or it's false, we should also return true. 170 | $has_consent = true; 171 | } elseif ( isset( $_COOKIE[ $cookie_name ] ) && 'allow' === $_COOKIE[ $cookie_name ] ) { 172 | // All other situations, return only true if value is allow. 173 | $has_consent = true; 174 | } else { 175 | $has_consent = false; 176 | } 177 | 178 | return apply_filters( 'wp_has_consent', $has_consent, $category, $requested_by ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- This is intended for Core. 179 | } 180 | 181 | /** 182 | * Retrieves cookie expiration. 183 | * 184 | * @return int Expiration in days. 185 | */ 186 | function wp_consent_api_cookie_expiration() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 187 | return apply_filters( 'wp_consent_api_cookie_expiration', WP_Consent_API::$config->cookie_expiration_days() ); 188 | } 189 | 190 | /** 191 | * Set accepted consent category. 192 | * 193 | * @since 1.0.0 194 | * 195 | * @param string $category The consent category. 196 | * @param string $value The value (either 'allow' or 'deny'). 197 | * 198 | * @return void 199 | */ 200 | function wp_set_consent( $category, $value ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 201 | $category = apply_filters( 'wp_set_consent_type', $category ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- This is intended for Core. 202 | $value = apply_filters( 'wp_set_consent_value', $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- This is intended for Core. 203 | 204 | $expiration = wp_consent_api_cookie_expiration() * DAY_IN_SECONDS; 205 | $category = wp_validate_consent_category( $category ); 206 | $value = wp_validate_consent_value( $value ); 207 | $prefix = WP_Consent_API::$config->consent_cookie_prefix(); 208 | 209 | setcookie( "{$prefix}_{$category}", $value, time() + $expiration, '/' ); 210 | } 211 | 212 | /** 213 | * Check if a plugin is registered for the WP Consent API. 214 | * 215 | * @since 1.0.0 216 | * 217 | * @param string $plugin The plugin basename. 218 | * 219 | * @return bool $registered 220 | */ 221 | function consent_api_registered( $plugin ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 222 | // We consider this plugin to comply ;). 223 | if ( strpos( $plugin, 'wp-consent-api.php' ) !== false ) { 224 | return true; 225 | } 226 | 227 | return apply_filters( "wp_consent_api_registered_{$plugin}", false ); 228 | } 229 | 230 | /** 231 | * Wrapper function for the registration of a cookie with WordPress. 232 | * 233 | * @param string $name The name of the cookie. 234 | * @param string $plugin_or_service Plugin or service that sets cookie (e.g. Google Maps). 235 | * @param string $category One of 'functional', 'preferences', 'statistics-anonymous', 'statistics', or 'marketing'. 236 | * @param string $expires Time until the cookie expires. 237 | * @param string $function What the cookie is meant to do (e.g. 'Store a unique User ID'). 238 | * @param string $collected_personal_data Type of personal data that is collected. If no personal data is collected, set to false. 239 | * @param bool $member_cookie Whether the cookie is relevant for members of the site only. 240 | * @param bool $administrator_cookie Whether the cookie is relevant for administrators only. 241 | * @param string $type One of 'HTTP', 'LOCALSTORAGE', or 'API'. 242 | * @param string|bool $domain Optional. Domain on which the cookie is set. Defaults to the current site URL. 243 | */ 244 | function wp_add_cookie_info( $name, $plugin_or_service, $category, $expires, $function, $collected_personal_data = '', $member_cookie = false, $administrator_cookie = false, $type = 'HTTP', $domain = false ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 245 | WP_Consent_API::$cookie_info->add_cookie_info( $name, $plugin_or_service, $category, $expires, $function, $collected_personal_data, $member_cookie, $administrator_cookie, $type, $domain ); 246 | } 247 | 248 | /** 249 | * Wrapper function to get cookie info for one specific cookie, or for all cookies registered. 250 | * 251 | * @param string|bool $name Optional. The cookie name. Default false, which returns all cookies. 252 | * 253 | * @return array 254 | */ 255 | function wp_get_cookie_info( $name = false ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 256 | return WP_Consent_API::$cookie_info->get_cookie_info( $name ); 257 | } 258 | 259 | /** 260 | * Wrapper function to set a cookie, taking into account actual user consent, if supported by plugins 261 | * 262 | * @param string $name 263 | * @param string $value 264 | * @param string $consent_category Functional, preferences, statistics-anonymous, statistics, marketing. 265 | * @param int $expires 266 | * @param string $path 267 | * @param string $domain 268 | * @param bool $secure 269 | * @param bool $httponly 270 | * 271 | * @return void 272 | */ 273 | function wp_set_cookie( $name, $value = '', $consent_category = '', $expires = 0, $path = '', $domain = '', $secure = false, $httponly = false ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is intended for Core. 274 | $name = sanitize_text_field( $name ); 275 | $value = sanitize_text_field( $value ); 276 | 277 | $expires = apply_filters( 'wp_setcookie_expires', intval( $expires ), $name, $value ); 278 | $path = apply_filters( 'wp_setcookie_path', sanitize_text_field( $path ), $name, $value ); 279 | $domain = apply_filters( 'wp_setcookie_domain', sanitize_text_field( $domain ), $name, $value ); 280 | 281 | $consent_category = apply_filters( 'wp_setcookie_category', wp_validate_consent_category( $consent_category ), $name, $value ); 282 | if ( empty( $consent_category ) ) { 283 | _doing_it_wrong( 'wp_setcookie', __( 'Missing consent category. A functional, preferences, statistics-anonymous, statistics or marketing category should be passed when using wp_setcookie.', 'wp-consent-api' ), '5.6' ); 284 | } 285 | 286 | if ( wp_has_consent( $consent_category ) ) { 287 | setcookie( $name, $value, $expires, $path, $domain, $secure, $httponly ); 288 | } 289 | } 290 | -------------------------------------------------------------------------------- /inc/class-wp-consent-api-config.php: -------------------------------------------------------------------------------- 1 | registered_cookies[ $name ] = array( 93 | 'plugin_or_service' => sanitize_text_field( $plugin_or_service ), 94 | 'category' => wp_validate_consent_category( $category ), 95 | 'expires' => sanitize_text_field( $expires ), 96 | 'function' => sanitize_text_field( $function ), 97 | 'collectedPersonalData' => sanitize_text_field( $collected_personal_data ), 98 | 'memberCookie' => (bool) $member_cookie, 99 | 'administratorCookie' => (bool) $administrator_cookie, 100 | 'domain' => esc_url_raw( $domain ), 101 | 'type' => sanitize_text_field( $type ), 102 | ); 103 | } 104 | 105 | 106 | /** 107 | * Get cookie info for one specific cookie, or for all cookies registered. 108 | * 109 | * @param string|bool $name The name of the cookie. 110 | * 111 | * @return array 112 | */ 113 | public function get_cookie_info( $name = false ) { 114 | if ( $name && isset( $this->registered_cookies[ $name ] ) ) { 115 | return $this->registered_cookies[ $name ]; 116 | } 117 | 118 | return $this->registered_cookies; 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /inc/class-wp-consent-api-site-health.php: -------------------------------------------------------------------------------- 1 | __( 'WP Consent API test', 'wp-consent-api' ), 70 | 'test' => array( $this, 'wp_consent_api_test' ), 71 | ); 72 | 73 | return $tests; 74 | } 75 | 76 | /** 77 | * Run the WP Consent API Site Health tests. 78 | * 79 | * @since 1.0.0 80 | * 81 | * @return array $result The WP Consent API Site Health tests results. 82 | */ 83 | public function wp_consent_api_test() { 84 | $plugins = get_option( 'active_plugins' ); 85 | $not_registered = array(); 86 | $plugins_without_registration = false; 87 | 88 | foreach ( $plugins as $plugin ) { 89 | if ( ! consent_api_registered( $plugin ) ) { 90 | $not_registered[] = $plugin; 91 | $plugins_without_registration = true; 92 | } 93 | } 94 | 95 | $result = array( 96 | 'label' => __( 'All plugins have declared to use the Consent API', 'wp-consent-api' ), 97 | 'status' => 'good', 98 | 'badge' => array( 99 | 'label' => __( 'Compliance', 'wp-consent-api' ), 100 | 'color' => 'blue', 101 | ), 102 | 'description' => sprintf( 103 | '

%s

', 104 | __( 'All plugins have declared in their code that they are following the guidelines from the WP Consent API. When used in combination with a Cookie Management plugin, this will improve compliancy for your site.', 'wp-consent-api' ) 105 | ), 106 | 'actions' => '', 107 | 'test' => 'wp-consent-api', 108 | ); 109 | 110 | if ( $plugins_without_registration ) { 111 | $result['status'] = 'recommended'; 112 | $result['label'] = __( 'One or more plugins are not conforming to the Consent API.', 'wp-consent-api' ); 113 | $result['description'] = '' . __( 'Not all plugins have declared to follow Consent API guidelines. Please contact the developer.', 'wp-consent-api' ) . ''; 114 | $result['actions'] = '

' . implode( '

', $not_registered ); 115 | } 116 | 117 | return $result; 118 | 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /inc/wordpress-comments-functions.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | A custom set of code standard rules to check for WordPress plugins. 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - vendor/szepeviktor/phpstan-wordpress/extension.neon 3 | parameters: 4 | bootstrapFiles: 5 | - tests/phpstan/bootstrap.php 6 | paths: 7 | - wp-consent-api.php 8 | - inc/ 9 | level: 5 10 | ignoreErrors: 11 | #- '#^Unsafe usage of new static#' 12 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | WP Consent API 2 | ====================== 3 | **Contributors**: RogierLankhorst, xkon, aurooba, mujuonly, phpgeek, paapst, aahulsebos, mundschenk-at 4 | 5 | **Tags**: consent, privacy 6 | 7 | **Requires at least**: 5.0 8 | 9 | **License**: GPL2 10 | 11 | **Tested up to**: 5.3 12 | 13 | **Requires PHP**: 5.6 14 | 15 | **Stable tag**: 1.0.0 16 | 17 | Description 18 | ----------- 19 | Consent API to read and register the current consent category, allowing consent management plugins and other plugins to work together, improving compliancy. 20 | 21 | What problem does this plugin solve? 22 | ------------------------------------ 23 | Currently it is possibly for a consent management plugin to block third party services like Facebook, Google Maps, Twitter, etc. But if a WordPress plugin places a PHP cookie, a consent management plugin cannot prevent this. 24 | 25 | Secondly, there are plugins that integrate tracking code on the clientside in javascript files that, when blocked, break the site. 26 | 27 | Or, if such a plugin's javascript is minified, causing the URL to be unrecognizable, it won't get detected by an automatic blocking script. 28 | 29 | Lastly, the blocking approach requires a list of all types of URL's that place cookies or use other means of tracking. A generic API where plugins adhere to can greatly 30 | facilitate a webmaster in getting a site compliant. 31 | 32 | Does usage of this API prevent third party services from tracking user data? 33 | ------------------------------------------------------------------ 34 | Primary this API is aimed at compliant first party cookies or other means of tracking by WordPress plugins. If such a plugin triggers for example Facebook, usage of this API will be of help. If a user manually embeds a facebook iframe, a cookie blocker is needed that initially disables the iframe and or scripts. 35 | 36 | Third party scripts have to blocked by a blocking functionality in a consent management plugin. To do this in core would be to intrusive, and is also not applicable to all users: only users with visitors from opt in regions such as the European Union require such a feature. Such a feature also has a risk of breaking things. Additionally, blocking these and showing a nice placeholder, requires even more sophisticated code, all of which should in my opinion not be part of WordPress core, for the same reasons. 37 | 38 | That said, the consent API can be used to decide if an iframe or script should be blocked. 39 | 40 | How does it work? 41 | ----------------- 42 | There are two indicators that together tell if consent is given for a certain consent category, e.g. "marketing": 43 | 44 | 1) the region based `consent_type`, which 45 | can be `optin`, `optout`, or other possible `consent_types`; 46 | 2) and the visitor's choice: `not set`, `allow` or `deny`. 47 | 48 | The `consent_type` is a function that wraps a filter,`wp_get_consent_type`. If there's no consent management plugin to set it, it will return `false`. This will cause all consent categories to return `true`, allowing cookies and other types of tracking for all categories. 49 | 50 | If `optin` is set using this filter, a category will only return `true` if the value of the visitor's choice is `allow`. 51 | 52 | If the region based `consent_type` is `optout`, it will return `true` if the visitor's choice is not set or is `allow`. 53 | 54 | Clientside, a consent management plugin can dynamically manipulate the consent type, and set the applicable categories. 55 | 56 | A plugin can use a hook to listen for changes, or check the value of a given category. 57 | 58 | Categories, and most other stuff can be extended with a filter. 59 | 60 | ## Existing integrations 61 | 62 | - Complianz https://github.com/really-simple-plugins/complianz-gdpr/ 63 | - Example plugin shipped with this plugin. The plugin basically consists of a shortcode, with a div that shows 64 | a tracking or not tracking message. No actual data tracking :) 65 | 66 | ## Demo site 67 | https://wpconsentapi.org/ 68 | 69 | plugins used to set this up: 70 | - Complianz 71 | - The example plugin https://github.com/rlankhorst/consent-api-example-plugin 72 | 73 | Code Examples 74 | ------------- 75 | If you have any other code suggestions, please PR them on GitHub! 76 | 77 | ### javascript, consent management plugin 78 | ```javascript 79 | //dynamically set consent type 80 | window.wp_consent_type = 'optin'; 81 | 82 | //dispatch event when consent type is defined 83 | let event = new CustomEvent('wp_consent_type_defined'); 84 | document.dispatchEvent( event ); 85 | 86 | //consent management plugin sets cookie when consent category value changes 87 | wp_set_consent('marketing', 'allow'); 88 | ``` 89 | 90 | ### javascript, cookie placing or tracking plugin 91 | ```javascript 92 | //listen to consent change event 93 | document.addEventListener("wp_listen_for_consent_change", function (e) { 94 | var changedConsentCategory = e.detail; 95 | for (var key in changedConsentCategory) { 96 | if (changedConsentCategory.hasOwnProperty(key)) { 97 | if (key === 'marketing' && changedConsentCategory[key] === 'allow') { 98 | console.log("just given consent, track user data") 99 | } 100 | } 101 | } 102 | }); 103 | 104 | //basic implementation of consent check: 105 | if (wp_has_consent('marketing')){ 106 | activateMarketing(); 107 | console.log("set marketing stuff now!"); 108 | } else { 109 | console.log("No marketing stuff please!"); 110 | } 111 | ``` 112 | ### PHP 113 | ```php 114 | //declare compliance with consent level API 115 | $plugin = plugin_basename( __FILE__ ); 116 | add_filter( "wp_consent_api_registered_{$plugin}", '__return_true' ); 117 | 118 | /** 119 | * Example how a plugin can register cookies with the consent API 120 | * These cookies can then be shown on the front-end, to the user, with wp_get_cookie_info() 121 | */ 122 | 123 | function my_wordpress_register_cookies(){ 124 | if ( function_exists( 'wp_add_cookie_info' ) ) { 125 | wp_add_cookie_info( 'AMP_token', 'AMP', 'marketing', __( 'Session' ), __( 'Store a unique User ID.' ), false, false, false ); 126 | } 127 | } 128 | add_action('plugins_loaded', 'my_wordpress_register_cookies'); 129 | 130 | 131 | //check if user has given marketing consent. Possible consent categories/purposes: 132 | //functional, preferences', statistics', statistics-anonymous', statistics', marketing', 133 | if (wp_has_consent('marketing')){ 134 | //do marketing stuff 135 | } 136 | 137 | //set the consent type (optin, optout, default false) 138 | add_filter( 'wp_get_consent_type', 'my_set_consenttype' , 10, 1 ); 139 | function my_set_consenttype($consenttype){ 140 | return 'optin'; 141 | } 142 | 143 | //filter consent categories types, example: remove the preferences category 144 | add_filter( 'wp_consent_categories', 'my_set_consentcategories' , 10, 1 ); 145 | function my_set_consentcategories($consentcategories){ 146 | unset($consentcategories['preferences']); 147 | return $consentcategories; 148 | } 149 | ``` 150 | 151 | Installation 152 | ------------ 153 | To install this plugin: 154 | 155 | 1. Download the plugin 156 | 2. Upload the plugin to the wp-content/plugins directory, 157 | 3. Go to “plugins” in your WordPress admin, then click activate. 158 | 159 | Frequently asked questions 160 | -------------------------- 161 | **Does this plugin block third party services from tracking user data?** 162 | 163 | No, this plugin provides a framework through which plugins can know if they are allowed to place cookies or use other means of tracking. 164 | The plugin requires both a consent management plugin for consent management, and a plugin that follows the consent level as can be read from this API. 165 | 166 | **How should I go about integrating my plugin?** 167 | 168 | Cookies or any other form of local storage can have a function and a purpose. A function is the particular task a cookie has. So a function can be "store the IP address". Purpose can be seen as the **Why** behind the function. So maybe the IP address is stored because it is needed for Statistics; or it is stored because it is used for marketing/tracking purposes; or it is needed for functional purposes. 169 | 170 | For each function you should consider what the purpose of that function is. There are 5 purpose categories: 171 | functional, statistics-anonymous, statistics, preferences, marketing. These are explained below. Your code should check if consent has been given for the applicable category. If no cookie banner plugin is active, 172 | the Consent API will always return with consent (true). 173 | Please check out the example plugin, and the above code examples. 174 | 175 | **What is the difference between the consent categories?** 176 | 177 | - statistics: 178 | 179 | Cookies or any other form of local storage that are used exclusively for statistical purposes (Analytics Cookies). 180 | 181 | - statistics-anonymous: 182 | 183 | Cookies or any other form of local storage that are used exclusively for anonymous statistical purposes (Anonymous Analytics Cookies), that are placed on a first party domain, and that do not allow identification of particular individuals. 184 | 185 | - marketing: 186 | 187 | Cookies or any other form of local storage required to create user profiles to send advertising or to track the user on a website or across websites for similar marketing purposes. 188 | 189 | - functional: 190 | 191 | The cookie or any other form of local storage is used for the sole purpose of carrying out the transmission of a 192 | communication over an electronic communications network; 193 | 194 | OR 195 | 196 | The technical storage or access is strictly necessary for the legitimate 197 | purpose of enabling the use of a specific service explicitly requested by the subscriber or 198 | user. If cookies are disabled, the requested functionality will not be available. This makes them essential functional cookies. 199 | 200 | - preferences: 201 | 202 | Cookies or any other form of local storage that can not be seen as statistics, statistics-anonymous, marketing or functional, and where the technical storage or access is necessary for the legitimate purpose of storing preferences. 203 | 204 | 205 | Changelog 206 | --------- 207 | ### 1.0.0 208 | 209 | Upgrade notice 210 | -------------- 211 | 212 | Screenshots 213 | ----------- 214 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === WP Consent API === 2 | Contributors: wordpressdotorg, RogierLankhorst, xkon, aurooba, mujuonly, phpgeekdk, paapst, aahulsebos, pputzer, markwolters, szepeviktor 3 | Tags: consent, privacy, cookies, api, compliance 4 | Requires at least: 5.0 5 | License: GPL2 6 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 7 | Tested up to: 6.8 8 | Requires PHP: 7.2 9 | Stable tag: 1.0.8 10 | 11 | Simple Consent API to read and register the current consent category. 12 | 13 | == Description == 14 | WP Consent API is a plugin that standardizes the communication of accepted consent categories between plugins. It requires a cookie banner plugin and, at least, one other plugin that supports the WP Consent API. 15 | 16 | With this plugin, all supporting plugins can use the same set of methods to read and register the current consent category, allowing consent management plugins and other plugins to work together, improving compliance with privacy laws. 17 | 18 | WARNING: the plugin itself will not handle consent. It will show you how many plugins you have without Consent API support and will improve compliance on your site by ensuring smooth communication between cookie banner plugins and plugins that set cookies or track user data. 19 | 20 | = What problem does this plugin solve? = 21 | Currently, it is possible for a consent management plugin to block third-party services like Facebook, Google Maps, Twitter, etc. But if a WordPress plugin places a PHP cookie, a consent management plugin cannot prevent this. 22 | 23 | Secondly, some plugins integrate the tracking code on the clientside in javascript files that, when blocked, break the site. 24 | 25 | Or, if such a plugin's javascript is minified, causing the URL to be unrecognizable and won't get detected by an automatic blocking script. 26 | 27 | Lastly, the blocking approach requires a list of all types of URL's that tracks data. A generic API where plugins adhere to can greatly 28 | facilitate a webmaster in getting a site compliant. 29 | 30 | = Does usage of this API prevent third-party services from tracking user data? = 31 | 32 | Primary this API is aimed at compliant first-party cookies or tracking by WordPress plugins. If such a plugin triggers, for example, Facebook, 33 | usage of this API will be of help. If a user embeds a Facebook iframe, a blocking tool is needed that initially disables the iframe and or scripts. 34 | 35 | Third-party scripts have to blocked by blocking functionality in a consent management plugin. To do this in core would be to intrusive, and is also not applicable to all users: only users with visitors from opt-in regions such as the European Union require such a feature. Such a feature also has a risk of breaking things. Additionally, blocking these and showing a nice placeholder requires even more sophisticated code, all of which should in my opinion not be part of WordPress core, for the same reasons. 36 | 37 | = How does it work? = 38 | There are two indicators that together tell if consent is given for a specific consent category, e.g., "marketing": 39 | 1) the region based consent_type, which 40 | can be opt-in, opt-out, or other possible consent_types; 41 | 2) and the visitor's choice: not set, allow, or deny. 42 | 43 | The consent_type is a function that wraps a filter, "wp_get_consent_type". If there's no consent management plugin to set it, it will return false. This will cause all consent categories to return true, allowing cookies to be set on all categories. 44 | 45 | If opt-in is set using this filter, a category will only return true if the value of the visitor's choice is "allow". 46 | 47 | If the region based consent_type is opt-out, it will return true if the visitor's choice is not set or is "allow". 48 | 49 | Clientside, a consent management plugin can dynamically manipulate the consent type and set several cookie categories. 50 | 51 | A plugin can use a hook to listen for changes or check the value of a given category. 52 | 53 | Categories and most other stuff can be extended with a filter. 54 | 55 | ## Existing integrations 56 | Categorized, and sorted alphabetically 57 | 58 | = Example plugin = 59 | - [Example plugin](https://github.com/rlankhorst/consent-api-example-plugin). The plugin basically consists of a shortcode, with a div that shows a tracking or not tracking message. No actual tracking is done :-) 60 | 61 | = Consent Management Providers = 62 | - [Beautiful Cookie Consent Banner](https://wordpress.org/plugins/beautiful-and-responsive-cookie-consent/). 63 | - [Complianz GDPR/CCPA](https://wordpress.org/plugins/complianz-gdpr/). 64 | - [Consent Studio](https://consent.studio/). 65 | - [Conzent](https://conzent.net/). 66 | - [Cookiebot](https://wordpress.org/plugins/cookiebot/). 67 | - [CookieHub](https://wordpress.org/plugins/cookiehub/). 68 | - [CookieYes – Cookie Banner for Cookie Consent](https://wordpress.org/plugins/cookie-law-info/). 69 | - [GDPR Cookie Compliance](https://wordpress.org/plugins/gdpr-cookie-compliance/). 70 | - [GDPR Cookie Consent Plugin - CCPA Ready](https://www.webtoffee.com/product/gdpr-cookie-consent/). 71 | - [Pressidium Cookie Consent](https://wordpress.org/plugins/pressidium-cookie-consent/). 72 | 73 | = Consent Requiring Plugins = 74 | - [AddToAny](https://wordpress.org/plugins/add-to-any/). 75 | - [AFL UTM Tracker Plugin](https://www.appfromlab.com/product/woocommerce-utm-tracker-plugin/). 76 | - [Burst Statistics](https://wordpress.org/plugins/burst-statistics/). 77 | - [Google Site Kit](https://wordpress.org/plugins/google-site-kit/). 78 | - [Pixel Manager for WooCommerce](https://wordpress.org/plugins/woocommerce-google-adwords-conversion-tracking-tag/). 79 | - [Woo](https://wordpress.org/plugins/woocommerce/). 80 | - [WP Statistics](https://wordpress.org/plugins/wp-statistics/). 81 | 82 | ## Demo site 83 | [wpconsentapi.org](https://wpconsentapi.org/) 84 | Below are the plugins used to set up the demo site: 85 | 86 | - Complianz 87 | - [The example plugin](https://github.com/rlankhorst/consent-api-example-plugin) 88 | 89 | = javascript, consent management plugin = 90 | ` 91 | //set consent type 92 | window.wp_consent_type = 'optin' 93 | 94 | //dispatch event when consent type is defined. This is useful if the region is detected server side, so the consent type is defined later during the pageload 95 | let event = new CustomEvent('wp_consent_type_defined'); 96 | document.dispatchEvent( event ); 97 | 98 | //consent management plugin sets cookie when consent category value changes 99 | wp_set_consent('marketing', 'allow'); 100 | ` 101 | = javascript, tracking plugin = 102 | ` 103 | //listen to consent change event 104 | document.addEventListener("wp_listen_for_consent_change", function (e) { 105 | var changedConsentCategory = e.detail; 106 | for (var key in changedConsentCategory) { 107 | if (changedConsentCategory.hasOwnProperty(key)) { 108 | if (key === 'marketing' && changedConsentCategory[key] === 'allow') { 109 | console.log("just given consent, track user") 110 | } 111 | } 112 | } 113 | }); 114 | 115 | //basic implementation of consent check: 116 | if (wp_has_consent('marketing')){ 117 | activateMarketing(); 118 | console.log("set marketing stuff now!"); 119 | } else { 120 | console.log("No marketing stuff please!"); 121 | } 122 | ` 123 | = PHP = 124 | ` 125 | //declare compliance with consent level API 126 | $plugin = plugin_basename( __FILE__ ); 127 | add_filter( "wp_consent_api_registered_{$plugin}", '__return_true' ); 128 | 129 | /** 130 | * Example how a plugin can register cookies with the consent API 131 | * These cookies can then be shown on the front-end, to the user, with wp_get_cookie_info() 132 | */ 133 | 134 | function my_wordpress_register_cookies(){ 135 | if ( function_exists( 'wp_add_cookie_info' ) ) { 136 | wp_add_cookie_info( 'AMP_token', 'AMP', 'marketing', __( 'Session' ), __( 'Store a unique User ID.' ) ); 137 | } 138 | } 139 | add_action('plugins_loaded', 'my_wordpress_register_cookies'); 140 | 141 | 142 | if (wp_has_consent('marketing')){ 143 | //do marketing stuff 144 | } 145 | ` 146 | Any code suggestions? We're on [GitHub](https://github.com/WordPress/wp-consent-level-api) as well! 147 | 148 | == Installation == 149 | To install this plugin: 150 | 151 | Download the plugin 152 | Upload the plugin to the wp-content/plugins directory, 153 | Go to "plugins" in your WordPress admin, then click activate. 154 | 155 | == Frequently asked questions == 156 | = Does this plugin block third party services from placing cookies? = 157 | No, this plugin provides a framework through which plugins can know if they are allowed to place cookies or track user data. 158 | The plugin requires both a consent management plugin for consent management and a plugin that follows the consent level as can be read from this API. 159 | = How should I go about integrating my plugin? = 160 | For each action that places cookies or tracks user data, you should consider what type of tracking takes place. There are 5 consent categories: 161 | functional, statistics-anonymous, statistics, preferences, marketing. These are explained below. Your code should check if consent has been given for the applicable category. If no cookie banner plugin is active, 162 | the Consent API will always return with consent (true). 163 | Please check out the example plugin, and the above code examples. 164 | = What is the difference between the consent categories? = 165 | 166 | Statistics: 167 | Cookies or any other form of local storage that are used exclusively for statistical purposes (Analytics Cookies). 168 | 169 | Statistics-anonymous: 170 | Cookies or any other form of local storage that are used exclusively for anonymous statistical purposes (Anonymous Analytics Cookies), that are placed on a first party domain, and that do not allow identification of particular individuals. 171 | 172 | Marketing: 173 | Cookies or any other form of local storage required to create user profiles to send advertising or to track the user on a website or across websites for similar marketing purposes. 174 | 175 | Functional: 176 | The cookie or any other form of local storage is used for the sole purpose of carrying out the transmission of a communication over an electronic communications network; 177 | 178 | OR 179 | 180 | The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user. If cookies are disabled, the requested functionality will not be available. This makes them essential functional cookies. 181 | 182 | Preferences: 183 | Cookies or any other form of local storage that can not be seen as statistics, statistics-anonymous, marketing or functional, and where the technical storage or access is necessary for the legitimate purpose of storing preferences. 184 | 185 | == Changelog == 186 | = 1.0.8 = 187 | * Updated tested up to 188 | * Dropped loading of translations, and loading of plugin_data, to prevent translation loading notices by WordPress 6.7, props @mujuonly 189 | * Dropped obsolete function wp_has_cookie_info, props @szepeviktor 190 | 191 | = 1.0.7 = 192 | * Tested up to 193 | 194 | = 1.0.7 = 195 | * Tested up to 196 | 197 | = 1.0.6 = 198 | * Tested up to 199 | 200 | = 1.0.5 = 201 | * tested up to 202 | 203 | = 1.0.4 = 204 | * Improvement: only fire on change event if an actual change was detected on the consent. 205 | 206 | = 1.0.3 = 207 | * Fix: wp_has_consent function contained a condition which caused it to always return true on serverside optin consent checks. 208 | 209 | = 1.0.2 = 210 | * Fix $collected_personal_data documentation, as $is_personal_data was deprecated (props @jazzsequence, https://github.com/rlankhorst/wp-consent-level-api/issues/54) 211 | * Fix the docblock so it reflects the actual value returned (props @jazzsequence, https://github.com/rlankhorst/wp-consent-level-api/issues/52) 212 | * Styling changes to site health 213 | * Code structure changes 214 | * Removed is_personal_data property, as it's implicitly contained in the personaldata property 215 | * Made cookie prefix filterable props @jazzsequence 216 | 217 | = 1.0.1 = 218 | * Cookiebot added as existing integration 219 | * Some small improvements to comply with WordPress coding styles 220 | 221 | = 1.0.0 = 222 | * changed consent_api_setcookie into consent_api_set_cookie for naming consistency with the getcookie method 223 | * added prefix in javascript set and get methods, to be compatible with PHP cookies, props @phpgeek 224 | * removed duplicate statistics category, props @phpgeek 225 | 226 | == Upgrade notice == 227 | 228 | == Screenshots == 229 | -------------------------------------------------------------------------------- /tests/phpstan/bootstrap.php: -------------------------------------------------------------------------------- 1 | setup_constants(); 121 | $this->includes(); 122 | 123 | self::$config = new WP_Consent_API_Config(); 124 | self::$site_health = new WP_Consent_API_Site_Health(); 125 | self::$cookie_info = new WP_Consent_API_Cookie_Info(); 126 | } 127 | 128 | /** 129 | * Define Consent API related constants. 130 | * 131 | * @since 1.0.0 132 | * 133 | * @return void 134 | */ 135 | private function setup_constants() { 136 | define( 'WP_CONSENT_API_URL', plugin_dir_url( __FILE__ ) ); 137 | define( 'WP_CONSENT_API_PATH', plugin_dir_path( __FILE__ ) ); 138 | define( 'WP_CONSENT_API_PLUGIN', plugin_basename( __FILE__ ) ); 139 | define( 'WP_CONSENT_API_VERSION', '1.0.8' ); 140 | define( 'WP_CONSENT_API_PLUGIN_FILE', __FILE__ ); 141 | } 142 | 143 | /** 144 | * Include the extra plugin files. 145 | * 146 | * @since 1.0.0 147 | * 148 | * @return void 149 | */ 150 | private function includes() { 151 | require_once WP_CONSENT_API_PATH . 'inc/class-wp-consent-api-config.php'; 152 | require_once WP_CONSENT_API_PATH . 'inc/class-wp-consent-api-cookie-info.php'; 153 | require_once WP_CONSENT_API_PATH . 'inc/class-wp-consent-api-site-health.php'; 154 | require_once WP_CONSENT_API_PATH . 'inc/api-functions.php'; 155 | require_once WP_CONSENT_API_PATH . 'inc/wordpress-comments-functions.php'; 156 | } 157 | } 158 | 159 | /** 160 | * Load the plugins main class. 161 | */ 162 | add_action( 'plugins_loaded', array( WP_Consent_API::class, 'get_instance' ), 9 ); 163 | } 164 | --------------------------------------------------------------------------------