├── .babelrc ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .stylelintrc ├── LICENSE ├── Readme.md ├── Readme.txt ├── admin ├── class-admin.php ├── class-api-settings-page.php ├── index.php └── templates │ ├── settings-page-general.php │ ├── settings-page-items.php │ └── settings-page-list.php ├── bin └── package.js ├── composer.json ├── composer.lock ├── data-list ├── class-data-list.php └── rest-routes │ └── data-list.php ├── decoupled-json-content.php ├── helpers ├── class-general-helper.php └── index.php ├── includes ├── class-deactivator.php ├── class-internationalization.php ├── class-loader.php ├── class-main.php └── index.php ├── index.php ├── lib ├── autoloader.php └── index.php ├── menu ├── class-menu.php └── rest-routes │ └── menu.php ├── package-lock.json ├── package.json ├── page ├── class-page.php └── rest-routes │ └── page.php ├── skin ├── assets │ ├── application.js │ └── scripts │ │ ├── regenerateDataTransients.js │ │ └── scripts.js └── public │ └── scripts │ └── djc-application.js ├── uninstall.php ├── webpack.config.js └── wp-config-simple.php /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["env", { 4 | "targets": { 5 | "browsers": ["last 2 versions", "not ie < 11", "android >= 4.2"] 6 | } 7 | }] 8 | ] 9 | } -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | skin/assets/scripts/vendors/*.js 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@infinumjs", 3 | 4 | "env": { 5 | 6 | # You can change environment depending on the project you are working on 7 | "browser": true, 8 | "node": false, 9 | "commonjs": true, 10 | "es6": true, 11 | "jquery": true 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /node_modules/ 3 | 4 | # Mac OS custom attribute store and thumbnails 5 | *.DS_Store 6 | ._* 7 | -------------------------------------------------------------------------------- /.stylelintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@infinumjs/stylelint-config", 3 | "ignoreFiles": "public/**/*.css", 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Decoupled JSON Content 2 | 3 | **Contributors**: dingo_bastard, mustra 4 | **Donate link**: https://eightshift.com/ 5 | **Tags**: json, decoupled, json, content, content, json content, react, angular, speed, fast json 6 | **Tested up to**: 4.9.4 7 | **Stable tag**: 1.0.0 8 | **Requires at least**: 4.4 9 | **Requires PHP**: 5.6 10 | **License**: GPLv2 or later 11 | **License URI**: http://www.gnu.org/licenses/gpl-2.0.html 12 | 13 | A faster alternative to the default REST API provided by WordPress for the usage by decoupled (headless) WordPress approach 14 | 15 | ## Description 16 | 17 | Since the WordPress version 4.4 WordPress added REST API to the core. With the version 4.7 came major improvements that basically allowed WordPress to be used as a unified Model/Controller part of the MVC application. The problem with the default approach to the decoupled WordPress (or headless as some call it) is that accessing the default REST endpoints in WordPress is really slow. The situation gets worse the more plugins you add to your WordPress installation. Hitting the particular endpoint means loading the entire WordPress core. And that is not acceptable if you want to have fast and responsive web applications. 18 | 19 | What our approach is using is twofold: We only load the minimum core capabilities, and manually call the necessary files for the necessary task we want to execute. After that, we also store the pulled data in a transient, which is used later on when fetching the data. Transients are great because once you set them up, they won't be called from the database directly, but from the server's memory, making the content fetching even faster. 20 | 21 | All the transients can be manually deleted, and we added filters to which you can hook with a third party plugin if you want to add or change something. 22 | 23 | We also added a useful custom menu route that can be used to tell the view part of the application what is the menu structure in the WordPress (menu routes are still not part of the WordPress REST API at the time of this plugins release). 24 | 25 | ## Installation 26 | 27 | 1. Place `decoupled-json-content` folder in the `/wp-content/plugins/` directory 28 | 2. Activate the plugin through the 'Plugins' menu in WordPress 29 | 3. Recreate all the endpoints in transient cache in Api Settings > Rebuild Cache 30 | 31 | # How to use 32 | 33 | After installing and activating a plugin you will have API Settings menu. This has two submenus - 'APIs List' which holds a list of available endpoints - the endpoints are general, e.g. for pages it can look something like `wp-content/plugins/decoupled-json-content/page/rest-routes/page.php?slug=&type=`. In addition to that, every page and post will have additional column in the post/page list which denote if the post/page has an existing cached JSON. Other submenu is called 'Rebuild Cache' which will rebuild all the caches for the pages/posts and custom post types in the database. 34 | 35 | ## Possible issues 36 | 37 | The transients are updated on page/post save using `save_post` hook. It could happen (although we didn't have this issue so far), that other plugins that add some content or data on `save_post` hook whouldn't have this data exposed in the JSON content. If this happens, just rebuld the cache after post save in the 'Rebuild Cache' menu. 38 | 39 | If you had content when installing this plugin you won't have any JSON for this post. In this case you also need to go to the 'Rebuild Cache' menu and resave it. 40 | 41 | ## Hooks 42 | 43 | We included several hooks that you can use to extend the functionality of the plugin: 44 | 45 | ### Menu: 46 | 47 | * `djc_remove_menu_prefix_slash` - Remove prefix slash in menu items, accepts boolean. Default is true. 48 | * `djc_set_menu_posts_slug` - Change the page slug for your blog posts. Default is 'blog' 49 | * `djc_set_general_endpoint` - Add new items the settings page general. The return value has to be array with keys: title, url and note(optional). 50 | 51 | ### Items: 52 | 53 | * `djc_set_items_featured_image` - Override the items featured image. 54 | * `djc_set_items_tags` - Override the items tags. 55 | * `djc_set_items_category` - Override the items category. 56 | * `djc_set_items_post_format` - Override the items format. 57 | * `djc_set_items_page_template` - Override the items template. 58 | * `djc_set_items_custom_fields` - Override the items custom fields. 59 | * `djc_set_items_append` - Append new data to items. The return value has to be array with key and value. 60 | * `djc_set_items_allowed_post_types` - Set post types you want to use of items. Default is 'post', 'page'. The return value has to be array. 61 | * `djc_set_items_endpoint` - Add new items the settings page items. The return value has to be array with keys: title, url and note(optional). 62 | * `djc_remove_items_updating` - Remove action to update items on change. 63 | 64 | ### List: 65 | 66 | * `djs_set_lists_endpoint_query` - set Wp_Query arguments you want to use in list. The return value has to be array with default WP Query arguments. Action-filter value from `djs_set_lists_endpoint` hook must be prepended to the `djs_set_lists_endpoint_query` hook. Check details in the example. 67 | * `djs_set_lists_endpoint` - Add new items the settings page list. The return value has to be array with keys: title, action-filter. 68 | * `djc_remove_lists_updating` - Remove action to update list on change. 69 | 70 | `djs_set_lists_endpoint_query` and `djs_set_lists_endpoint` go in combination. 71 | 72 | Example: 73 | 74 | ```php 75 | function new_enpodint_function( $default ) { 76 | $default[] = array( 77 | 'title' => 'New List', 78 | 'action-filter' => 'new_action_filter', 79 | ); 80 | 81 | return $default; 82 | } 83 | 84 | add_filter( 'djs_set_lists_endpoint', 'new_enpodint_function' ); 85 | 86 | function new_args_function() { 87 | return array( 88 | 'post_type' => 'page', 89 | 'posts_per_page' => 1, 90 | ); 91 | } 92 | 93 | add_filter( 'djs_set_lists_endpoint_query_`new_action_filter`', 'new_args_function' ); 94 | ``` 95 | 96 | ## Changelog 97 | 98 | = 1.0 = 99 | 100 | * Initial release 101 | 102 | ## Credits 103 | 104 | JSON post parser is maintained and sponsored by 105 | [Infinum](https://www.infinum.co). 106 | 107 | 108 | 109 | ## License 110 | 111 | Decoupled JSON Content is Copyright © 2017 Infinum. It is free software, and may be redistributed under the terms specified in the LICENSE file. 112 | 113 | ## Donate 114 | 115 | We don't need your donations. Give it to charity instead. And check out our work at [Infinum](https://www.infinum.co). 116 | 117 | ## Screenshots 118 | 119 | 1. Settings: List of all available endpoints 120 | 2. Settings: Action to rebuild all endpoints to transient 121 | 3. Endpoint: Page json endpoint 122 | 4. Endpoint: Menu json endpoint 123 | 5. Listing: Column added to see if endpoint is cached and available 124 | -------------------------------------------------------------------------------- /Readme.txt: -------------------------------------------------------------------------------- 1 | === Decoupled JSON Content === 2 | 3 | Contributors: dingo_bastard, mustra 4 | Donate link: https://eightshift.com/ 5 | Tags: json, decoupled, json, content, content, json content, react, angular, speed, fast json 6 | Tested up to: 4.9.4 7 | Stable tag: 1.0.0 8 | Requires at least: 4.4 9 | Requires PHP: 5.6 10 | License: GPLv2 or later 11 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 12 | 13 | A faster alternative to the default REST API provided by WordPress for the usage by decoupled (headless) WordPress approach 14 | 15 | == Description == 16 | 17 | Since the WordPress version 4.4 WordPress added REST API to the core. With the version 4.7 came major improvements that basically allowed WordPress to be used as a unified Model/Controller part of the MVC application. The problem with the default approach to the decoupled WordPress (or headless as some call it) is that accessing the default REST endpoints in WordPress is really slow. The situation gets worse the more plugins you add to your WordPress installation. Hitting the particular endpoint means loading the entire WordPress core. And that is not acceptable if you want to have fast and responsive web applications. 18 | 19 | What our approach is using is twofold: We only load the minimum core capabilities, and manually call the necessary files for the necessary task we want to execute. After that, we also store the pulled data in a transient, which is used later on when fetching the data. Transients are great because once you set them up, they won't be called from the database directly, but from the server's memory, making the content fetching even faster. 20 | 21 | All the transients can be manually deleted, and we added filters to which you can hook with a third party plugin if you want to add or change something. 22 | 23 | We also added a useful custom menu route that can be used to tell the view part of the application what is the menu structure in the WordPress (menu routes are still not part of the WordPress REST API at the time of this plugins release). 24 | 25 | == Installation == 26 | 27 | 1. Place `decoupled-json-content` folder in the `/wp-content/plugins/` directory 28 | 2. Activate the plugin through the 'Plugins' menu in WordPress 29 | 3. Recreate all the endpoints in transient cache in Api Settings > Rebuild Cache 30 | 31 | # How to use 32 | 33 | After installing and activating a plugin you will have API Settings menu. This has two submenus - 'APIs List' which holds a list of available endpoints - the endpoints are general, e.g. for pages it can look something like `wp-content/plugins/decoupled-json-content/page/rest-routes/page.php?slug=&type=`. In addition to that, every page and post will have additional column in the post/page list which denote if the post/page has an existing cached JSON. Other submenu is called 'Rebuild Cache' which will rebuild all the caches for the pages/posts and custom post types in the database. 34 | 35 | == Possible issues == 36 | 37 | The transients are updated on page/post save using `save_post` hook. It could happen (although we didn't have this issue so far), that other plugins that add some content or data on `save_post` hook whouldn't have this data exposed in the JSON content. If this happens, just rebuld the cache after post save in the 'Rebuild Cache' menu. 38 | 39 | If you had content when installing this plugin you won't have any JSON for this post. In this case you also need to go to the 'Rebuild Cache' menu and resave it. 40 | 41 | == Hooks == 42 | 43 | We included several hooks that you can use to extend the functionality of the plugin: 44 | 45 | Menu: 46 | 47 | * `djc_remove_menu_prefix_slash` - Remove prefix slash in menu items, accepts boolean. Default is true. 48 | * `djc_set_menu_posts_slug` - Change the page slug for your blog posts. Default is 'blog' 49 | * `djc_set_general_endpoint` - Add new items the settings page general. The return value has to be array with keys: title, url and note(optional). 50 | 51 | Items: 52 | 53 | * `djc_set_items_featured_image` - Override the items featured image. 54 | * `djc_set_items_tags` - Override the items tags. 55 | * `djc_set_items_category` - Override the items category. 56 | * `djc_set_items_post_format` - Override the items format. 57 | * `djc_set_items_page_template` - Override the items template. 58 | * `djc_set_items_custom_fields` - Override the items custom fields. 59 | * `djc_set_items_append` - Append new data to items. The return value has to be array with key and value. 60 | * `djc_set_items_allowed_post_types` - Set post types you want to use of items. Default is 'post', 'page'. The return value has to be array. 61 | * `djc_set_items_endpoint` - Add new items the settings page items. The return value has to be array with keys: title, url and note(optional). 62 | * `djc_remove_items_updating` - Remove action to update items on change. 63 | 64 | List: 65 | 66 | * `djs_set_lists_endpoint_query` - set Wp_Query arguments you want to use in list. The return value has to be array with default WP Query arguments. Action-filter value from `djs_set_lists_endpoint` hook must be prepended to the `djs_set_lists_endpoint_query` hook. Check details in the example. 67 | * `djs_set_lists_endpoint` - Add new items the settings page list. The return value has to be array with keys: title, action-filter. 68 | * `djc_remove_lists_updating` - Remove action to update list on change. 69 | 70 | `djs_set_lists_endpoint_query` and `djs_set_lists_endpoint` go in combination. 71 | 72 | Example: 73 | 74 | ```php 75 | function new_enpodint_function( $default ) { 76 | $default[] = array( 77 | 'title' => 'New List', 78 | 'action-filter' => 'new_action_filter', 79 | ); 80 | 81 | return $default; 82 | } 83 | 84 | add_filter( 'djs_set_lists_endpoint', 'new_enpodint_function' ); 85 | 86 | function new_args_function() { 87 | return array( 88 | 'post_type' => 'page', 89 | 'posts_per_page' => 1, 90 | ); 91 | } 92 | 93 | add_filter( 'djs_set_lists_endpoint_query_`new_action_filter`', 'new_args_function' ); 94 | ``` 95 | 96 | == Changelog == 97 | 98 | = 1.0 = 99 | 100 | * Initial release 101 | 102 | == Credits == 103 | 104 | JSON post parser is maintained and sponsored by 105 | [Infinum](https://www.infinum.co). 106 | 107 | 108 | 109 | == License == 110 | 111 | Decoupled JSON Content is Copyright © 2017 Infinum. It is free software, and may be redistributed under the terms specified in the LICENSE file. 112 | 113 | == Donate == 114 | 115 | We don't need your donations. Give it to charity instead. And check out our work at [Infinum](https://www.infinum.co). 116 | 117 | == Screenshots == 118 | 119 | 1. Settings: List of all available endpoints 120 | 2. Settings: Action to rebuild all endpoints to transient 121 | 3. Endpoint: Page json endpoint 122 | 4. Endpoint: Menu json endpoint 123 | 5. Listing: Column added to see if endpoint is cached and available 124 | -------------------------------------------------------------------------------- /admin/class-admin.php: -------------------------------------------------------------------------------- 1 | plugin_name = $plugin_info['plugin_name']; 54 | $this->plugin_version = $plugin_info['plugin_version']; 55 | 56 | $this->general_helper = new General_Helpers\General_Helper(); 57 | } 58 | 59 | /** 60 | * Register the JavaScript for the admin area. 61 | * 62 | * @since 1.0.0 63 | */ 64 | public function enqueue_scripts() { 65 | 66 | $main_script = '/skin/public/scripts/djc-application.js'; 67 | wp_register_script( $this->plugin_name . '-scripts', plugin_dir_url( __DIR__ ) . $main_script, array(), $this->general_helper->get_assets_version( $main_script ) ); 68 | wp_enqueue_script( $this->plugin_name . '-scripts' ); 69 | 70 | // Glbal variables for ajax and translations. 71 | wp_localize_script( 72 | $this->plugin_name . '-scripts', 'djcLocalization', array( 73 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), 74 | 'confirmRebuildAction' => esc_html__( 'Are you sure you want to rebuild all transients?', 'decoupled_json_content' ), 75 | ) 76 | ); 77 | 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /admin/class-api-settings-page.php: -------------------------------------------------------------------------------- 1 | plugin_name = $plugin_info['plugin_name']; 74 | $this->plugin_version = $plugin_info['plugin_version']; 75 | } 76 | 77 | /** 78 | * Register Setting page to sidebar navigation 79 | * 80 | * @since 1.0.0 81 | */ 82 | public function register_settings_page() { 83 | 84 | add_menu_page( 85 | esc_html__( 'API Settings', 'decoupled_json_content' ), 86 | esc_html__( 'API Settings', 'decoupled_json_content' ), 87 | 'manage_options', 88 | $this->settings_page_main_slug 89 | ); 90 | 91 | add_submenu_page( 92 | $this->settings_page_main_slug, 93 | esc_html__( 'General', 'decoupled_json_content' ), 94 | esc_html__( 'General', 'decoupled_json_content' ), 95 | 'manage_options', 96 | $this->settings_page_main_slug, 97 | array( $this, 'get_settings_page_general' ) 98 | ); 99 | 100 | add_submenu_page( 101 | $this->settings_page_main_slug, 102 | esc_html__( 'Items', 'decoupled_json_content' ), 103 | esc_html__( 'Items', 'decoupled_json_content' ), 104 | 'manage_options', 105 | $this->settings_page_items_slug, 106 | array( $this, 'get_settings_page_items' ) 107 | ); 108 | 109 | add_submenu_page( 110 | $this->settings_page_main_slug, 111 | esc_html__( 'List', 'decoupled_json_content' ), 112 | esc_html__( 'List', 'decoupled_json_content' ), 113 | 'manage_options', 114 | $this->settings_page_list_slug, 115 | array( $this, 'get_settings_page_list' ) 116 | ); 117 | } 118 | 119 | /** 120 | * Return array of all avaiable endpoints for General List 121 | * 122 | * @return array 123 | * 124 | * @since 1.0.0 125 | */ 126 | public function get_settings_page_data_general() { 127 | $default_endpoints = array( 128 | array( 129 | 'title' => 'Menus', 130 | 'url' => get_home_url() . '/wp-content/plugins/' . $this->plugin_name . '/menu/rest-routes/menu.php', 131 | 'note' => wp_kses_post( 'Transient data is set on admin init.
Cache updated on Menu update.', 'decoupled_json_content' ), 132 | ), 133 | ); 134 | 135 | // Allow developers to add new items to list. 136 | if ( has_filter( 'djc_set_general_endpoint' ) ) { 137 | $appended_endpoints = apply_filters( 'djc_set_general_endpoint', $default_endpoints ); 138 | 139 | // Appended Items must be multidimensional array. 140 | if ( is_array( $appended_endpoints ) ) { 141 | $default_endpoints = $appended_endpoints; 142 | } 143 | } 144 | 145 | return $default_endpoints; 146 | } 147 | 148 | /** 149 | * Return array of all avaiable endpoints for Items list. 150 | * 151 | * @return array 152 | * 153 | * @since 1.0.0 154 | */ 155 | public function get_settings_page_data_items() { 156 | $default_endpoints = array( 157 | array( 158 | 'title' => 'Individual Item', 159 | 'url' => get_home_url() . '/wp-content/plugins/' . $this->plugin_name . '/page/rest-routes/page.php?slug=&type=', 160 | 'note' => wp_kses_post( __( 'Transient data is set on post/page/custom_post_type save.
Cache updated on post/page/custom_post_type save or on Rebuild Items button.', 'decoupled_json_content' ) ), 161 | ), 162 | ); 163 | 164 | // Allow developers to add new items to list. 165 | if ( has_filter( 'djc_set_items_endpoint' ) ) { 166 | $appended_endpoints = apply_filters( 'djc_set_items_endpoint', $default_endpoints ); 167 | 168 | // Appended Items must be multidimensional array. 169 | if ( is_array( $appended_endpoints ) ) { 170 | $default_endpoints = $appended_endpoints; 171 | } 172 | } 173 | 174 | return $default_endpoints; 175 | } 176 | 177 | /** 178 | * Return array of all avaiable endpoints for data list. 179 | * 180 | * @return array 181 | * 182 | * @since 1.0.0 183 | */ 184 | public function get_settings_page_data_list() { 185 | $default_endpoints = array( 186 | array( 187 | 'title' => 'Rebuild Default Posts List', 188 | 'action-filter' => 'default', 189 | 'post-type' => 'post', 190 | ), 191 | ); 192 | 193 | // Allow developers to add new items to list. 194 | if ( has_filter( 'djs_set_lists_endpoint' ) ) { 195 | $appended_endpoints = apply_filters( 'djs_set_lists_endpoint', $default_endpoints ); 196 | 197 | // Appended Items must be multidimensional array. 198 | if ( is_array( $appended_endpoints ) ) { 199 | $default_endpoints = $appended_endpoints; 200 | } 201 | } 202 | 203 | return $default_endpoints; 204 | } 205 | 206 | 207 | /** 208 | * Get template view from partial file for general page. 209 | * 210 | * @since 1.0.0 211 | */ 212 | public function get_settings_page_general() { 213 | $list = $this->get_settings_page_data_general(); 214 | $general_helper = new General_Helpers\General_Helper(); 215 | require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/templates/settings-page-general.php'; 216 | unset( $list ); 217 | } 218 | 219 | /** 220 | * Get template view from partial file for items page. 221 | * 222 | * @since 1.0.0 223 | */ 224 | public function get_settings_page_items() { 225 | $list = $this->get_settings_page_data_items(); 226 | $general_helper = new General_Helpers\General_Helper(); 227 | require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/templates/settings-page-items.php'; 228 | unset( $list ); 229 | } 230 | 231 | /** 232 | * Get template view from partial file for list page. 233 | * 234 | * @since 1.0.0 235 | */ 236 | public function get_settings_page_list() { 237 | $list = $this->get_settings_page_data_list(); 238 | $general_helper = new General_Helpers\General_Helper(); 239 | require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/templates/settings-page-list.php'; 240 | unset( $list ); 241 | } 242 | 243 | } 244 | -------------------------------------------------------------------------------- /admin/index.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /admin/templates/settings-page-general.php: -------------------------------------------------------------------------------- 1 | 10 |
11 |

12 |

13 |

14 |

15 |

16 |
17 | 18 |
    19 | 20 | get_array_value( 'url', $list_item ); 22 | $title = $general_helper->get_array_value( 'title', $list_item ); 23 | $note = $general_helper->get_array_value( 'note', $list_item ); 24 | ?> 25 | 26 |
  • 27 | 28 | 29 | 30 | 31 |
    32 | 33 | 34 |
  • 35 | 36 | 37 |
38 | 39 |
40 | -------------------------------------------------------------------------------- /admin/templates/settings-page-items.php: -------------------------------------------------------------------------------- 1 | 10 |
11 |

12 |

13 |

post_type and slug as a GET parameter.', 'decoupled_json_content' ); ?>

14 |

15 |

16 |
17 | 18 |
    19 | 20 | get_array_value( 'url', $list_item ); 22 | $title = $general_helper->get_array_value( 'title', $list_item ); 23 | $note = $general_helper->get_array_value( 'note', $list_item ); 24 | ?> 25 | 26 |
  • 27 | 28 | 29 | 30 | 31 |
    32 | 33 | 34 |
  • 35 | 36 | 37 |
38 | 39 |
40 |

41 |
42 |

43 |

44 | 45 | 46 |
47 | -------------------------------------------------------------------------------- /admin/templates/settings-page-list.php: -------------------------------------------------------------------------------- 1 | 10 |
11 |

12 |

13 |

14 |

15 |

16 |
17 |
18 | 19 |
    20 | 21 | get_array_value( 'post-type', $list_item ); 23 | $action_filter = $general_helper->get_array_value( 'action-filter', $list_item ); 24 | $title = $general_helper->get_array_value( 'title', $list_item ); 25 | ?> 26 | 27 | plugin_name . '/data-list/rest-routes/data-list.php?post-type=' . $post_type . '&filter=' . $action_filter; 29 | ?> 30 |
  • 31 | 32 | 33 | 34 |   35 | 38 |
  • 39 | 40 | 41 |
42 | 43 | 44 |
45 | -------------------------------------------------------------------------------- /bin/package.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | 6 | // Copy the build plugin in a separate folder called /build. 7 | // Remove developer source files, and leave only build files - check with guys on wp.org slack what is needed. 8 | // exclude node_modules/ and vendor/ folder from the build 9 | // Zip the build file to be plugin ready. 10 | // Delete the unzipped stuff from the folder. 11 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require-dev": { 3 | "dealerdirect/phpcodesniffer-composer-installer": "0.5", 4 | "infinum/coding-standards-wp": "0.4.1" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "8da74cf9ff0b93d46d5892149d151b12", 8 | "packages": [], 9 | "packages-dev": [ 10 | { 11 | "name": "dealerdirect/phpcodesniffer-composer-installer", 12 | "version": "v0.5.0", 13 | "source": { 14 | "type": "git", 15 | "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", 16 | "reference": "e749410375ff6fb7a040a68878c656c2e610b132" 17 | }, 18 | "dist": { 19 | "type": "zip", 20 | "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/e749410375ff6fb7a040a68878c656c2e610b132", 21 | "reference": "e749410375ff6fb7a040a68878c656c2e610b132", 22 | "shasum": "" 23 | }, 24 | "require": { 25 | "composer-plugin-api": "^1.0", 26 | "php": "^5.3|^7", 27 | "squizlabs/php_codesniffer": "^2|^3" 28 | }, 29 | "require-dev": { 30 | "composer/composer": "*", 31 | "phpcompatibility/php-compatibility": "^9.0", 32 | "sensiolabs/security-checker": "^4.1.0" 33 | }, 34 | "type": "composer-plugin", 35 | "extra": { 36 | "class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" 37 | }, 38 | "autoload": { 39 | "psr-4": { 40 | "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" 41 | } 42 | }, 43 | "notification-url": "https://packagist.org/downloads/", 44 | "license": [ 45 | "MIT" 46 | ], 47 | "authors": [ 48 | { 49 | "name": "Franck Nijhof", 50 | "email": "franck.nijhof@dealerdirect.com", 51 | "homepage": "http://www.frenck.nl", 52 | "role": "Developer / IT Manager" 53 | } 54 | ], 55 | "description": "PHP_CodeSniffer Standards Composer Installer Plugin", 56 | "homepage": "http://www.dealerdirect.com", 57 | "keywords": [ 58 | "PHPCodeSniffer", 59 | "PHP_CodeSniffer", 60 | "code quality", 61 | "codesniffer", 62 | "composer", 63 | "installer", 64 | "phpcs", 65 | "plugin", 66 | "qa", 67 | "quality", 68 | "standard", 69 | "standards", 70 | "style guide", 71 | "stylecheck", 72 | "tests" 73 | ], 74 | "time": "2018-10-26T13:21:45+00:00" 75 | }, 76 | { 77 | "name": "infinum/coding-standards-wp", 78 | "version": "0.4.1", 79 | "source": { 80 | "type": "git", 81 | "url": "https://github.com/infinum/coding-standards-wp.git", 82 | "reference": "87aa3608eb064308291f8f7f3d3d30a8fd1ea296" 83 | }, 84 | "dist": { 85 | "type": "zip", 86 | "url": "https://api.github.com/repos/infinum/coding-standards-wp/zipball/87aa3608eb064308291f8f7f3d3d30a8fd1ea296", 87 | "reference": "87aa3608eb064308291f8f7f3d3d30a8fd1ea296", 88 | "shasum": "" 89 | }, 90 | "require": { 91 | "php": ">=5.6", 92 | "phpcompatibility/phpcompatibility-wp": "^2.0", 93 | "squizlabs/php_codesniffer": "^3.3.0", 94 | "wp-coding-standards/wpcs": "^1.0.0" 95 | }, 96 | "require-dev": { 97 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3", 98 | "phpcompatibility/php-compatibility": "^9.0", 99 | "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0", 100 | "roave/security-advisories": "dev-master" 101 | }, 102 | "suggest": { 103 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." 104 | }, 105 | "type": "phpcodesniffer-standard", 106 | "notification-url": "https://packagist.org/downloads/", 107 | "license": [ 108 | "MIT" 109 | ], 110 | "authors": [ 111 | { 112 | "name": "Contributors", 113 | "homepage": "https://github.com/infinum/coding-standards-wp/graphs/contributors" 114 | } 115 | ], 116 | "description": "Infinum WordPress Coding Standards", 117 | "homepage": "https://github.com/infinum/coding-standards-wp", 118 | "keywords": [ 119 | "Eightshift", 120 | "Infinum", 121 | "phpcs", 122 | "standards", 123 | "wordpress" 124 | ], 125 | "time": "2018-11-15T12:49:58+00:00" 126 | }, 127 | { 128 | "name": "phpcompatibility/php-compatibility", 129 | "version": "9.1.1", 130 | "source": { 131 | "type": "git", 132 | "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", 133 | "reference": "2b63c5d284ab8857f7b1d5c240ddb507a6b2293c" 134 | }, 135 | "dist": { 136 | "type": "zip", 137 | "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/2b63c5d284ab8857f7b1d5c240ddb507a6b2293c", 138 | "reference": "2b63c5d284ab8857f7b1d5c240ddb507a6b2293c", 139 | "shasum": "" 140 | }, 141 | "require": { 142 | "php": ">=5.3", 143 | "squizlabs/php_codesniffer": "^2.3 || ^3.0.2" 144 | }, 145 | "conflict": { 146 | "squizlabs/php_codesniffer": "2.6.2" 147 | }, 148 | "require-dev": { 149 | "phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0" 150 | }, 151 | "suggest": { 152 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.", 153 | "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." 154 | }, 155 | "type": "phpcodesniffer-standard", 156 | "notification-url": "https://packagist.org/downloads/", 157 | "license": [ 158 | "LGPL-3.0-or-later" 159 | ], 160 | "authors": [ 161 | { 162 | "name": "Contributors", 163 | "homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors" 164 | }, 165 | { 166 | "name": "Wim Godden", 167 | "homepage": "https://github.com/wimg", 168 | "role": "lead" 169 | }, 170 | { 171 | "name": "Juliette Reinders Folmer", 172 | "homepage": "https://github.com/jrfnl", 173 | "role": "lead" 174 | } 175 | ], 176 | "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.", 177 | "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", 178 | "keywords": [ 179 | "compatibility", 180 | "phpcs", 181 | "standards" 182 | ], 183 | "time": "2018-12-30T23:16:27+00:00" 184 | }, 185 | { 186 | "name": "phpcompatibility/phpcompatibility-paragonie", 187 | "version": "1.0.1", 188 | "source": { 189 | "type": "git", 190 | "url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git", 191 | "reference": "9160de79fcd683b5c99e9c4133728d91529753ea" 192 | }, 193 | "dist": { 194 | "type": "zip", 195 | "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/9160de79fcd683b5c99e9c4133728d91529753ea", 196 | "reference": "9160de79fcd683b5c99e9c4133728d91529753ea", 197 | "shasum": "" 198 | }, 199 | "require": { 200 | "phpcompatibility/php-compatibility": "^9.0" 201 | }, 202 | "require-dev": { 203 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4" 204 | }, 205 | "suggest": { 206 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", 207 | "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." 208 | }, 209 | "type": "phpcodesniffer-standard", 210 | "notification-url": "https://packagist.org/downloads/", 211 | "license": [ 212 | "LGPL-3.0-or-later" 213 | ], 214 | "authors": [ 215 | { 216 | "name": "Wim Godden", 217 | "role": "lead" 218 | }, 219 | { 220 | "name": "Juliette Reinders Folmer", 221 | "role": "lead" 222 | } 223 | ], 224 | "description": "A set of rulesets for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by the Paragonie polyfill libraries.", 225 | "homepage": "http://phpcompatibility.com/", 226 | "keywords": [ 227 | "compatibility", 228 | "paragonie", 229 | "phpcs", 230 | "polyfill", 231 | "standards" 232 | ], 233 | "time": "2018-12-16T19:10:44+00:00" 234 | }, 235 | { 236 | "name": "phpcompatibility/phpcompatibility-wp", 237 | "version": "2.0.0", 238 | "source": { 239 | "type": "git", 240 | "url": "https://github.com/PHPCompatibility/PHPCompatibilityWP.git", 241 | "reference": "cb303f0067cd5b366a41d4fb0e254fb40ff02efd" 242 | }, 243 | "dist": { 244 | "type": "zip", 245 | "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityWP/zipball/cb303f0067cd5b366a41d4fb0e254fb40ff02efd", 246 | "reference": "cb303f0067cd5b366a41d4fb0e254fb40ff02efd", 247 | "shasum": "" 248 | }, 249 | "require": { 250 | "phpcompatibility/php-compatibility": "^9.0", 251 | "phpcompatibility/phpcompatibility-paragonie": "^1.0" 252 | }, 253 | "require-dev": { 254 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3" 255 | }, 256 | "suggest": { 257 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", 258 | "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." 259 | }, 260 | "type": "phpcodesniffer-standard", 261 | "notification-url": "https://packagist.org/downloads/", 262 | "license": [ 263 | "LGPL-3.0-or-later" 264 | ], 265 | "authors": [ 266 | { 267 | "name": "Wim Godden", 268 | "role": "lead" 269 | }, 270 | { 271 | "name": "Juliette Reinders Folmer", 272 | "role": "lead" 273 | } 274 | ], 275 | "description": "A ruleset for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by WordPress.", 276 | "homepage": "http://phpcompatibility.com/", 277 | "keywords": [ 278 | "compatibility", 279 | "phpcs", 280 | "standards", 281 | "wordpress" 282 | ], 283 | "time": "2018-10-07T18:31:37+00:00" 284 | }, 285 | { 286 | "name": "squizlabs/php_codesniffer", 287 | "version": "3.4.2", 288 | "source": { 289 | "type": "git", 290 | "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", 291 | "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8" 292 | }, 293 | "dist": { 294 | "type": "zip", 295 | "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", 296 | "reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8", 297 | "shasum": "" 298 | }, 299 | "require": { 300 | "ext-simplexml": "*", 301 | "ext-tokenizer": "*", 302 | "ext-xmlwriter": "*", 303 | "php": ">=5.4.0" 304 | }, 305 | "require-dev": { 306 | "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" 307 | }, 308 | "bin": [ 309 | "bin/phpcs", 310 | "bin/phpcbf" 311 | ], 312 | "type": "library", 313 | "extra": { 314 | "branch-alias": { 315 | "dev-master": "3.x-dev" 316 | } 317 | }, 318 | "notification-url": "https://packagist.org/downloads/", 319 | "license": [ 320 | "BSD-3-Clause" 321 | ], 322 | "authors": [ 323 | { 324 | "name": "Greg Sherwood", 325 | "role": "lead" 326 | } 327 | ], 328 | "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", 329 | "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", 330 | "keywords": [ 331 | "phpcs", 332 | "standards" 333 | ], 334 | "time": "2019-04-10T23:49:02+00:00" 335 | }, 336 | { 337 | "name": "wp-coding-standards/wpcs", 338 | "version": "1.2.1", 339 | "source": { 340 | "type": "git", 341 | "url": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git", 342 | "reference": "f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c" 343 | }, 344 | "dist": { 345 | "type": "zip", 346 | "url": "https://api.github.com/repos/WordPress-Coding-Standards/WordPress-Coding-Standards/zipball/f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c", 347 | "reference": "f328bcafd97377e8e5e5d7b244d5ddbf301a3a5c", 348 | "shasum": "" 349 | }, 350 | "require": { 351 | "php": ">=5.3", 352 | "squizlabs/php_codesniffer": "^2.9.0 || ^3.0.2" 353 | }, 354 | "require-dev": { 355 | "phpcompatibility/php-compatibility": "^9.0" 356 | }, 357 | "suggest": { 358 | "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." 359 | }, 360 | "type": "phpcodesniffer-standard", 361 | "notification-url": "https://packagist.org/downloads/", 362 | "license": [ 363 | "MIT" 364 | ], 365 | "authors": [ 366 | { 367 | "name": "Contributors", 368 | "homepage": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/graphs/contributors" 369 | } 370 | ], 371 | "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", 372 | "keywords": [ 373 | "phpcs", 374 | "standards", 375 | "wordpress" 376 | ], 377 | "time": "2018-12-18T09:43:51+00:00" 378 | } 379 | ], 380 | "aliases": [], 381 | "minimum-stability": "stable", 382 | "stability-flags": [], 383 | "prefer-stable": false, 384 | "prefer-lowest": false, 385 | "platform": [], 386 | "platform-dev": [] 387 | } 388 | -------------------------------------------------------------------------------- /data-list/class-data-list.php: -------------------------------------------------------------------------------- 1 | plugin_name = $plugin_info['plugin_name']; 55 | $this->plugin_version = $plugin_info['plugin_version']; 56 | } 57 | 58 | /** 59 | * Build final json by getting the order from transient build by button in list settings page. 60 | * 61 | * @param string $filter Transient name provided by filter paremetar in url. 62 | * @return array 63 | * 64 | * @since 1.0.0 65 | */ 66 | public function get_data_list( $post_type = null, $filter = null ) { 67 | if ( ! ( $post_type || $filter ) ) { 68 | return false; 69 | } 70 | 71 | $page = new Page\Page(); 72 | 73 | $cache_name = $this->get_cache_name( $post_type, $filter ); 74 | if ( ! $cache_name ) { 75 | return false; 76 | } 77 | 78 | $posts = get_transient( $cache_name ); 79 | if ( ! $posts ) { 80 | return false; 81 | } 82 | 83 | $posts_output = array(); 84 | foreach( $posts as $post ) { 85 | if( $post ) { 86 | $get_transient_data = get_transient( $page->get_page_cache_name_by_slug( $post['slug'], $post['type'] ) ); 87 | if( $get_transient_data === false ) { 88 | return false; 89 | } 90 | 91 | $posts_output[] = json_decode( $get_transient_data ); 92 | } 93 | } 94 | 95 | return $posts_output; 96 | } 97 | 98 | /** 99 | * Get cache name. 100 | * 101 | * @param string $action_filter Action filter name provided by filter paremetar in url. 102 | * @return string 103 | * 104 | * @since 1.0.0 105 | */ 106 | public function get_cache_name( $post_type = null, $action_filter = null ) { 107 | if ( ! ( $post_type || $action_filter ) ) { 108 | return false; 109 | } 110 | 111 | return $this->list_cache_name . '_' . $post_type . '_' . $action_filter; 112 | } 113 | 114 | /** 115 | * Get default posts list arguments. 116 | * 117 | * @return array 118 | * 119 | * @since 1.0.0 120 | */ 121 | public function get_default_args() { 122 | return array( 123 | 'post_type' => 'post', 124 | 'posts_per_page' => 5000, 125 | 'post_status' => 'publish' 126 | ); 127 | } 128 | 129 | /** 130 | * Get arguments list for query. 131 | * 132 | * @param string $filter Add custom filter to override the default. Filter hook is created by default name and prefixed filter provider as arg. 133 | * @return array 134 | * 135 | * @since 1.0.0 136 | */ 137 | public function get_args( $filter = null ) { 138 | 139 | $default = $this->get_default_args(); 140 | $action_filter = ''; 141 | 142 | if( $filter ) { 143 | $action_filter = '_' . $filter; 144 | } 145 | 146 | // Allow developers to add new items to array. 147 | if ( has_filter( 'djs_set_lists_endpoint_query' . $action_filter ) ) { 148 | $filtered = apply_filters( 'djs_set_lists_endpoint_query' . $action_filter, $default ); 149 | 150 | // Must be array. 151 | if ( is_array( $filtered ) ) { 152 | $default = $filtered; 153 | } 154 | } 155 | 156 | return $default; 157 | } 158 | 159 | /** 160 | * Set transient data to be later use for sort ordering. 161 | * 162 | * @param string $action_filter Action filter name provided by filter paremetar in url. 163 | * 164 | * @since 1.0.0 165 | */ 166 | public function set_transient( $postType = null, $action_filter = null ) { 167 | if ( current_user_can( 'edit_users' ) ) { 168 | if ( ! ( $post_type || $action_filter ) ) { 169 | return false; 170 | } 171 | 172 | if( ! empty( $action_filter ) ) { 173 | $action_filter = str_replace(' ', '_', $action_filter); 174 | $action_filter = str_replace('-', '_', $action_filter); 175 | } 176 | 177 | $output_array = array(); 178 | $args = $this->get_args( $action_filter ); 179 | 180 | if( ! $args ) { 181 | return false; 182 | } 183 | 184 | $the_query = new \WP_Query( $args ); 185 | 186 | if ( $the_query->have_posts() ) { 187 | while ( $the_query->have_posts() ) { 188 | $the_query->the_post(); 189 | $post_slug = $the_query->post->post_name; 190 | $post_type = $the_query->post->post_type; 191 | 192 | $output_array[] = array( 193 | 'slug' => $post_slug, 194 | 'type' => $post_type, 195 | ); 196 | } 197 | wp_reset_postdata(); 198 | } 199 | 200 | $cache_name = $this->get_cache_name( $postType, $action_filter ); 201 | if ( ! $cache_name ) { 202 | return false; 203 | } 204 | 205 | set_transient( $cache_name, $output_array, 0 ); 206 | } 207 | } 208 | 209 | /** 210 | * Update list to transient for caching on action hooks save_post. 211 | * 212 | * @param int $post_id Saved Post ID provided by action hook. 213 | * 214 | * @since 1.0.0 215 | */ 216 | public function update_page_transient( $post_id ) { 217 | 218 | // Remove updating on save. 219 | if ( has_filter( 'djc_remove_lists_updating' ) ) { 220 | $remove_updating = apply_filters( 'djc_remove_lists_updating', '' ); 221 | 222 | if( $remove_updating === true ) { 223 | return false; 224 | } 225 | } 226 | 227 | $post_status = get_post_status( $post_id ); 228 | $post_type = get_post_type( $post_id ); 229 | $transient_name = '_transient_' . $this->list_cache_name . '_' . $post_type . '_'; 230 | 231 | if ( $post_status === 'auto-draft' || $post_status === 'inherit' ) { 232 | return false; 233 | } else { 234 | 235 | // Loop all options to find transient items. 236 | foreach( wp_load_alloptions() as $key => $value ) { 237 | 238 | // Regex to find transient matching post type. 239 | if( preg_match( '/^' . $transient_name . '/', $key ) ) { 240 | 241 | // Strip string to get action filter name. 242 | $action_filter = str_replace( $transient_name, '', $key ); 243 | 244 | // Update transient. 245 | $this->set_transient( $post_type, $action_filter ); 246 | } 247 | } 248 | } 249 | } 250 | 251 | 252 | /** 253 | * Ajax function to rebuild all data list transients. 254 | * 255 | * @since 1.0.0 256 | */ 257 | public function djc_rebuild_lists_transients_ajax() { 258 | $general_helper = new General_Helpers\General_Helper(); 259 | $action_filter = ''; 260 | 261 | if ( ! isset( $_POST['djcRebuildNonce'] ) && ! wp_verify_nonce( sanitize_key( $_POST['djcRebuildNonce'] ), 'djc_rebuild_nonce_action' ) ) { 262 | wp_send_json( $general_helper->set_msg_array( 'error', 'Check your nonce!' ) ); 263 | } 264 | 265 | if ( isset( $_REQUEST['actionFilter'] ) ) { // WPCS: input var ok; CSRF ok. 266 | $action_filter = sanitize_text_field( wp_unslash( $_REQUEST['actionFilter'] ) ); // WPCS: input var ok; CSRF ok. 267 | } 268 | 269 | if ( isset( $_REQUEST['postType'] ) ) { // WPCS: input var ok; CSRF ok. 270 | $postType = sanitize_text_field( wp_unslash( $_REQUEST['postType'] ) ); // WPCS: input var ok; CSRF ok. 271 | } 272 | 273 | $this->set_transient( $postType, $action_filter ); 274 | 275 | wp_send_json( $general_helper->set_msg_array( 'success', 'Success in rebuilding transients for cache!' ) ); 276 | } 277 | } 278 | -------------------------------------------------------------------------------- /data-list/rest-routes/data-list.php: -------------------------------------------------------------------------------- 1 | set_msg_array( 'error', 'Error, type is missing!' ) ); 31 | } 32 | 33 | // Check Filter. 34 | if ( isset( $_GET['filter'] ) && ! empty( $_GET['filter'] ) ) { // WPCS: input var ok; CSRF ok. 35 | $filter = sanitize_text_field( wp_unslash( $_GET['filter'] ) ); // WPCS: input var ok; CSRF ok. 36 | } else { 37 | wp_send_json( $general_helper->set_msg_array( 'error', 'Error, filter is missing!' ) ); 38 | } 39 | 40 | $cache = $data_list->get_data_list( $post_type, $filter ); 41 | 42 | if ( $cache === false ) { 43 | wp_send_json( $general_helper->set_msg_array( 'error', 'Error, there is a problem with your configuration or pages/posts are it is not cached correctly. Please check your configuration, rebuilding cache and try again!' ) ); 44 | } 45 | 46 | wp_send_json( $cache ); 47 | -------------------------------------------------------------------------------- /decoupled-json-content.php: -------------------------------------------------------------------------------- 1 | run(); 78 | } 79 | 80 | init_plugin(); 81 | -------------------------------------------------------------------------------- /helpers/class-general-helper.php: -------------------------------------------------------------------------------- 1 | plugin_name = $plugin_info['plugin_name']; 43 | $this->plugin_version = $plugin_info['plugin_version']; 44 | } 45 | 46 | /** 47 | * Check if array has key and return its value if true. 48 | * Useful if you want to be sure that key exists and return empty if it doesn't. 49 | * 50 | * @param string $key Array key to check. 51 | * @param array $array Array in which the key should be checked. 52 | * @return string Value of the key if it exists, empty string if not. 53 | * 54 | * @since 1.0.0 55 | */ 56 | public function get_array_value( $key, $array ) { 57 | return ( gettype( $array ) === 'array' && array_key_exists( $key, $array ) ) ? $array[ $key ] : ''; 58 | } 59 | 60 | /** 61 | * Set array to be used globaly for messages 62 | * 63 | * @param string $status Status of message. 64 | * @param string $msg Message content. 65 | * @param array $data Data callback. 66 | * @return array 67 | * 68 | * @since 1.0.0 69 | */ 70 | public function set_msg_array( $status = null, $msg = null, $data = null ) { 71 | if ( $status === 'error' ) { 72 | http_response_code( 400 ); 73 | } 74 | 75 | return array( 76 | 'status' => $status, 77 | 'msg' => $msg, 78 | 'data' => $data, 79 | ); 80 | } 81 | 82 | /** 83 | * Return timestamp when file is changes. 84 | * This is used for cache busting assets. 85 | * 86 | * @param string $filename File name you want to get timestamp from. 87 | * @return init Timestamp. 88 | * 89 | * @since 1.0.0 90 | */ 91 | public function get_assets_version( $filename = null ) { 92 | if ( ! $filename ) { 93 | return false; 94 | } 95 | 96 | $file_location = get_template_directory() . $filename; 97 | 98 | if ( ! file_exists( $file_location ) ) { 99 | return; 100 | } 101 | 102 | return filemtime( $file_location ); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /helpers/index.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /includes/class-deactivator.php: -------------------------------------------------------------------------------- 1 | plugin_name = $plugin_info['plugin_name']; 46 | $this->plugin_version = $plugin_info['plugin_version']; 47 | } 48 | 49 | /** 50 | * Load the plugin text domain for translation. 51 | * 52 | * @since 1.0.0 53 | */ 54 | public function load_plugin_textdomain() { 55 | load_plugin_textdomain( 56 | $this->plugin_name, 57 | false, 58 | dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' 59 | ); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /includes/class-loader.php: -------------------------------------------------------------------------------- 1 | actions = array(); 45 | $this->filters = array(); 46 | } 47 | 48 | /** 49 | * Add a new action to the collection to be registered with WordPress. 50 | * 51 | * @since 1.0.0 52 | * @param string $hook The name of the WordPress action that is being registered. 53 | * @param object $component A reference to the instance of the object on which the action is defined. 54 | * @param string $callback The name of the function definition on the $component. 55 | * @param int $priority Optional. The priority at which the function should be fired. Default is 10. 56 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. 57 | */ 58 | public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { 59 | $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); 60 | } 61 | 62 | /** 63 | * Remove action from the registered collection of hooks. 64 | * 65 | * @since 1.0.0 66 | * @param string $hook The name of the WordPress action that is being registered. 67 | * @param string $function_to_remove The name of the function definition on the $component. 68 | * @param int $priority Optional. The priority at which the function should be fired. Default is 10. 69 | */ 70 | public function remove_action( $hook, $function_to_remove, $priority = 10 ) { 71 | remove_action( $hook, $function_to_remove, $priority ); 72 | } 73 | 74 | /** 75 | * Add a new filter to the collection to be registered with WordPress. 76 | * 77 | * @since 1.0.0 78 | * @param string $hook The name of the WordPress filter that is being registered. 79 | * @param object $component A reference to the instance of the object on which the filter is defined. 80 | * @param string $callback The name of the function definition on the $component. 81 | * @param int $priority Optional. The priority at which the function should be fired. Default is 10. 82 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. 83 | */ 84 | public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { 85 | $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); 86 | } 87 | 88 | /** 89 | * Remove filter from the registered collection of hooks. 90 | * 91 | * @since 1.0.0 92 | * @param string $hook The name of the WordPress filter that is being registered. 93 | * @param string $function_to_remove The name of the function definition on the $component. 94 | * @param int $priority Optional. The priority at which the function should be fired. Default is 10. 95 | */ 96 | public function remove_filter( $hook, $function_to_remove, $priority = 10 ) { 97 | remove_filter( $hook, $function_to_remove, $priority ); 98 | } 99 | 100 | /** 101 | * A utility function that is used to register the actions and hooks into a single 102 | * collection. 103 | * 104 | * @since 1.0.0 105 | * @access private 106 | * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). 107 | * @param string $hook The name of the WordPress filter that is being registered. 108 | * @param object $component A reference to the instance of the object on which the filter is defined. 109 | * @param string $callback The name of the function definition on the $component. 110 | * @param int $priority The priority at which the function should be fired. 111 | * @param int $accepted_args The number of arguments that should be passed to the $callback. 112 | * @return array The collection of actions and filters registered with WordPress. 113 | */ 114 | private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { 115 | $hooks[] = array( 116 | 'hook' => $hook, 117 | 'component' => $component, 118 | 'callback' => $callback, 119 | 'priority' => $priority, 120 | 'accepted_args' => $accepted_args, 121 | ); 122 | 123 | return $hooks; 124 | } 125 | 126 | /** 127 | * Register the filters and actions with WordPress. 128 | * 129 | * @since 1.0.0 130 | */ 131 | public function run() { 132 | foreach ( $this->filters as $hook ) { 133 | add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); 134 | } 135 | 136 | foreach ( $this->actions as $hook ) { 137 | add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); 138 | } 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /includes/class-main.php: -------------------------------------------------------------------------------- 1 | plugin_version = DJC_PLUGIN_VERSION; 74 | } else { 75 | $this->plugin_version = '1.0.0'; 76 | } 77 | 78 | if ( defined( 'DJC_PLUGIN_NAME' ) ) { 79 | $this->plugin_name = DJC_PLUGIN_NAME; 80 | } else { 81 | $this->plugin_name = 'decoupled-json-content'; 82 | } 83 | 84 | $this->load_dependencies(); 85 | $this->set_locale(); 86 | $this->define_admin_hooks(); 87 | $this->define_menu_hooks(); 88 | $this->define_page_hooks(); 89 | $this->define_data_list_hooks(); 90 | } 91 | 92 | /** 93 | * Load the required dependencies. 94 | * 95 | * Create an instance of the loader which will be used to register the hooks 96 | * with WordPress. 97 | * 98 | * @since 1.0.0 99 | */ 100 | private function load_dependencies() { 101 | $this->loader = new Loader(); 102 | } 103 | 104 | /** 105 | * Define the locale for this plugin for internationalization. 106 | * 107 | * Uses the Decoupled_Json_Content_i18n class in order to set the domain and to register the hook 108 | * with WordPress. 109 | * 110 | * @since 1.0.0 111 | */ 112 | private function set_locale() { 113 | $plugin_i18n = new Internationalization( $this->get_plugin_info() ); 114 | 115 | $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); 116 | } 117 | 118 | /** 119 | * Register all of the hooks related to the admin area functionality 120 | * of the plugin. 121 | * 122 | * @since 1.0.0 123 | */ 124 | private function define_admin_hooks() { 125 | $admin = new Admin\Admin( $this->get_plugin_info() ); 126 | $api_settings_page = new Admin\Api_Settings_Page( $this->get_plugin_info() ); 127 | 128 | // Admin. 129 | $this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_scripts' ); 130 | 131 | // Api settings page. 132 | $this->loader->add_action( 'admin_menu', $api_settings_page, 'register_settings_page' ); 133 | } 134 | 135 | /** 136 | * Register hooks for Menu functionality 137 | * 138 | * @since 1.0.0 139 | */ 140 | private function define_menu_hooks() { 141 | $menu = new Menu\Menu( $this->get_plugin_info() ); 142 | 143 | // Menues. 144 | $this->loader->add_action( 'admin_head', $menu, 'set_page_transient' ); 145 | $this->loader->add_action( 'wp_update_nav_menu', $menu, 'clear_cache' ); 146 | 147 | } 148 | 149 | /** 150 | * Register hooks for Page functionality 151 | * 152 | * @since 1.0.0 153 | */ 154 | private function define_page_hooks() { 155 | $page = new Page\Page( $this->get_plugin_info() ); 156 | 157 | // Page. 158 | $this->loader->add_action( 'save_post', $page, 'update_page_transient' ); 159 | 160 | // Ajax callbacks. 161 | $this->loader->add_action( 'wp_ajax_nopriv_djc_rebuild_items_transients_ajax', $page, 'djc_rebuild_items_transients_ajax' ); 162 | $this->loader->add_action( 'wp_ajax_djc_rebuild_items_transients_ajax', $page, 'djc_rebuild_items_transients_ajax' ); 163 | 164 | // Listing and single items. 165 | $this->loader->add_action( 'manage_pages_custom_column', $page, 'add_admin_columns_content', 10, 2 ); 166 | $this->loader->add_filter( 'manage_pages_columns', $page, 'add_admin_columns' ); 167 | $this->loader->add_action( 'manage_posts_custom_column', $page, 'add_admin_columns_content', 10, 2 ); 168 | $this->loader->add_filter( 'manage_posts_columns', $page, 'add_admin_columns' ); 169 | $this->loader->add_filter( 'post_submitbox_misc_actions', $page, 'add_publish_meta_options' ); 170 | } 171 | 172 | /** 173 | * Register hooks for Data-List functionality 174 | * 175 | * @since 1.0.0 176 | */ 177 | private function define_data_list_hooks() { 178 | $data_list = new Data_List\Data_List( $this->get_plugin_info() ); 179 | 180 | // Data List. 181 | $this->loader->add_action( 'save_post', $data_list, 'update_page_transient' ); 182 | 183 | // Ajax callbacks. 184 | $this->loader->add_action( 'wp_ajax_nopriv_djc_rebuild_lists_transients_ajax', $data_list, 'djc_rebuild_lists_transients_ajax' ); 185 | $this->loader->add_action( 'wp_ajax_djc_rebuild_lists_transients_ajax', $data_list, 'djc_rebuild_lists_transients_ajax' ); 186 | 187 | } 188 | 189 | /** 190 | * Run the loader to execute all of the hooks with WordPress. 191 | * 192 | * @since 1.0.0 193 | */ 194 | public function run() { 195 | $this->loader->run(); 196 | } 197 | 198 | /** 199 | * The reference to the class that orchestrates the hooks. 200 | * 201 | * @return Loader Orchestrates the hooks. 202 | * 203 | * @since 1.0.0 204 | */ 205 | public function get_loader() { 206 | return $this->loader; 207 | } 208 | 209 | /** 210 | * The name used to uniquely identify it within the context of 211 | * WordPress and to define internationalization functionality. 212 | * 213 | * @return string Plugin name. 214 | * 215 | * @since 1.0.0 216 | */ 217 | public function get_plugin_name() { 218 | return $this->plugin_name; 219 | } 220 | 221 | /** 222 | * Retrieve the version number. 223 | * 224 | * @return string Plugin version number. 225 | * 226 | * @since 1.0.0 227 | */ 228 | public function get_plugin_version() { 229 | return $this->plugin_version; 230 | } 231 | 232 | /** 233 | * Retrieve the plugin info array. 234 | * 235 | * @return array Plugin info array. 236 | * 237 | * @since 1.0.0 238 | */ 239 | public function get_plugin_info() { 240 | return array( 241 | 'plugin_name' => $this->plugin_name, 242 | 'plugin_version' => $this->plugin_version, 243 | ); 244 | } 245 | 246 | } 247 | -------------------------------------------------------------------------------- /includes/index.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /lib/autoloader.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /menu/class-menu.php: -------------------------------------------------------------------------------- 1 | plugin_name = $plugin_info['plugin_name']; 62 | $this->plugin_version = $plugin_info['plugin_version']; 63 | } 64 | 65 | /** 66 | * Return all menu poistions 67 | * 68 | * @return array Of menu positions with name and slug. 69 | * 70 | * @since 1.0.0 71 | */ 72 | public function get_menu_positions() { 73 | return get_registered_nav_menus(); 74 | } 75 | 76 | /** 77 | * Set default posts slug used for post url 78 | * 79 | * @return string 80 | * 81 | * @since 1.0.0 82 | */ 83 | public function get_default_posts_slug() { 84 | return apply_filters( 'djc_set_menu_posts_slug', $this->posts_slug ); 85 | } 86 | 87 | /** 88 | * Return menu items assigned to menu locations 89 | * With changed url from absolute to relative path 90 | * 91 | * @param string $theme_location Menu location configured in get_menu_positions() function. 92 | * @return array Menu items styled for json-api. 93 | * 94 | * @since 1.0.0 95 | */ 96 | public function get_assigned_filtered_menu_items( $theme_location ) { 97 | if ( ! $theme_location ) { 98 | return false; 99 | } 100 | 101 | $locations = get_nav_menu_locations(); 102 | if ( ! $locations ) { 103 | return false; 104 | } 105 | 106 | if ( ! isset( $locations[ $theme_location ] ) ) { 107 | return false; 108 | } 109 | 110 | $menu = get_term( $locations[ $theme_location ], 'nav_menu' ); 111 | 112 | // Return menu items as object. 113 | $menu_items = wp_get_nav_menu_items( $menu->term_id ); 114 | 115 | // Filter output to match requirements. 116 | $menu_items_oputput = array(); 117 | foreach ( $menu_items as $menu_item ) { 118 | 119 | // Filter hook to remove prefix slash. 120 | $prefix_slash = apply_filters( 'djc_remove_menu_prefix_slash', true ); 121 | 122 | // Remove absolute url. 123 | $url = str_replace( get_home_url(), '', $menu_item->url ); 124 | 125 | // Append post prefix if type is post. 126 | if ( $menu_item->object === 'post' ) { 127 | $url = '/' . $this->get_default_posts_slug() . $url; 128 | } 129 | 130 | // Remove last slash. 131 | $url = rtrim( $url, '/' ); 132 | 133 | // Remove first slash. 134 | if ( $prefix_slash ) { 135 | $url = ltrim( $url, '/' ); 136 | } 137 | 138 | // If is custom link just output url. 139 | if ( $menu_item->object === 'custom' ) { 140 | $slug = $menu_item->url; 141 | } else { 142 | // If is internal find post slug. 143 | $slug = get_post( $menu_item->object_id ); 144 | $slug = $slug->post_name; 145 | } 146 | 147 | // If empty add slash on the end, for home page. 148 | if ( empty( $url ) ) { 149 | $url = '/'; 150 | } 151 | 152 | $menu_items_oputput[] = array( 153 | 'id' => $menu_item->ID, 154 | 'title' => $menu_item->title, 155 | 'url' => $url, 156 | 'slug' => $slug, 157 | 'parent' => (int) $menu_item->menu_item_parent, 158 | 'target' => $menu_item->target === '_blank', 159 | 'attr_title' => $menu_item->attr_title, 160 | 'xfn' => $menu_item->xfn, 161 | 'description' => $menu_item->description, 162 | 'classes' => implode( ' ', $menu_item->classes ), 163 | 'type' => $menu_item->object, 164 | ); 165 | } 166 | 167 | return $menu_items_oputput; 168 | } 169 | 170 | /** 171 | * Return array with all menus and their items 172 | * 173 | * @return array Menu array styled for json-api. 174 | * 175 | * @since 1.0.0 176 | */ 177 | public function get_full_filtered_menus() { 178 | $menu_positions = $this->get_menu_positions(); 179 | 180 | $menu_output = array(); 181 | foreach ( $menu_positions as $menu_position_key => $menu_position_value ) { 182 | $menu_output[] = array( 183 | 'name' => $menu_position_value, 184 | 'position' => $menu_position_key, 185 | 'items' => $this->get_assigned_filtered_menu_items( $menu_position_key ), 186 | ); 187 | } 188 | 189 | return $menu_output; 190 | } 191 | 192 | /** 193 | * Return Menus in JSON format 194 | * 195 | * @return json 196 | * 197 | * @since 1.0.0 198 | */ 199 | public function get_json_menus() { 200 | return wp_json_encode( $this->get_full_filtered_menus() ); 201 | } 202 | 203 | /** 204 | * Set Menus to transient for caching 205 | * 206 | * @since 1.0.0 207 | */ 208 | public function set_page_transient() { 209 | $cache_name = $this->menu_cache_name; 210 | $cache = get_transient( $cache_name ); 211 | 212 | if ( $cache === false ) { 213 | $cache = $this->get_json_menus(); 214 | 215 | set_transient( $cache_name, $cache, 0 ); 216 | } 217 | } 218 | 219 | /** 220 | * Chear cache on menu open 221 | * 222 | * @since 1.0.0 223 | */ 224 | public function clear_cache() { 225 | $cache_name = $this->menu_cache_name; 226 | 227 | $screen = get_current_screen(); 228 | 229 | if ( is_admin() && $screen->base === 'nav-menus' ) { 230 | delete_transient( $cache_name ); 231 | } 232 | } 233 | } 234 | -------------------------------------------------------------------------------- /menu/rest-routes/menu.php: -------------------------------------------------------------------------------- 1 | menu_cache_name ); 25 | 26 | if ( $cache === false ) { 27 | wp_send_json( $general_helper->set_msg_array( 'error', 'Error, menu does not exist in cache. Please rebuild cache.' ) ); 28 | } 29 | 30 | wp_send_json( json_decode( $cache ) ); 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "decoupled-json-content", 3 | "version": "1.0.0", 4 | "author": "Infinum", 5 | "private": true, 6 | "main": "", 7 | "scripts": { 8 | "__eslintPlugin": "eslint skin/assets/", 9 | "precommitJs": "npm run __eslintPlugin", 10 | "precommit": "npm run precommitJs", 11 | "start": "webpack --progress --watch --display-error-details --display-reasons", 12 | "package": "bin/package.js", 13 | "build": "NODE_ENV=production webpack --progress" 14 | }, 15 | "devDependencies": { 16 | "@infinumjs/eslint-config": "^1.1.0", 17 | "babel-core": "^6.26.3", 18 | "babel-eslint": "^10.0.1", 19 | "babel-loader": "^7.1.2", 20 | "babel-preset-env": "^1.7.0", 21 | "clean-webpack-plugin": "^0.1.18", 22 | "eslint": "^5.16.0", 23 | "eslint-plugin-import": "^2.8.0", 24 | "expose-loader": "^0.7.4", 25 | "imports-loader": "^0.7.1", 26 | "uglifyjs-webpack-plugin": "^1.1.8", 27 | "webpack": "^4.30.0" 28 | }, 29 | "dependencies": { 30 | "babel-polyfill": "^6.26.0", 31 | "bugsnag-js": "^4.2.0", 32 | "jquery": "^3.3.1", 33 | "whatwg-fetch": "^2.0.3" 34 | }, 35 | "browserslist": [ 36 | "android >= 4.2", 37 | "not ie < 11", 38 | "last 2 versions", 39 | "Safari >= 8" 40 | ] 41 | } 42 | -------------------------------------------------------------------------------- /page/class-page.php: -------------------------------------------------------------------------------- 1 | plugin_name = $plugin_info['plugin_name']; 54 | $this->plugin_version = $plugin_info['plugin_version']; 55 | } 56 | 57 | /** 58 | * Helper function to get Page cache name for transient by post slug and type. 59 | * 60 | * @param string $post_slug Page Slug to save. 61 | * @param string $post_type Page Type to save. 62 | * @return string 63 | * 64 | * @since 1.0.0 65 | */ 66 | public function get_page_cache_name_by_slug( $post_slug = null, $post_type = null ) { 67 | if ( ! ( $post_slug || $post_type ) ) { 68 | return false; 69 | } 70 | 71 | $post_slug = str_replace( '__trashed', '', $post_slug ); 72 | 73 | return $this->page_cache_name . '_' . $post_type . '_' . $post_slug; 74 | } 75 | 76 | /** 77 | * Helper function to get Page details by Post ID. 78 | * 79 | * @param int $post_id Page/Post ID. 80 | * @return array 81 | * 82 | * @since 1.0.0 83 | */ 84 | public function get_page_details_by_id( $post_id = null ) { 85 | if ( ! $post_id ) { 86 | return false; 87 | } 88 | 89 | $post = get_post( $post_id ); 90 | if ( ! $post ) { 91 | return false; 92 | } 93 | 94 | return array( 95 | 'slug' => $post->post_name, 96 | 'type' => $post->post_type, 97 | ); 98 | } 99 | 100 | /** 101 | * Helper function to get cache name from post ID 102 | * 103 | * @param int $post_id Page/Post ID. 104 | * @return string 105 | * 106 | * @since 1.0.0 107 | */ 108 | public function get_page_cache_name_by_id( $post_id = null ) { 109 | if ( ! $post_id ) { 110 | return false; 111 | } 112 | 113 | $post = $this->get_page_details_by_id( $post_id ); 114 | if ( ! $post ) { 115 | return false; 116 | } 117 | 118 | return $this->get_page_cache_name_by_slug( $post['slug'], $post['type'] ); 119 | } 120 | 121 | /** 122 | * Helper function to get endpoint link by providing post ID 123 | * 124 | * @param int $post_id Page/Post ID. 125 | * @return string 126 | * 127 | * @since 1.0.0 128 | */ 129 | public function get_api_endpoint_link_by_id( $post_id = null ) { 130 | if ( ! $post_id ) { 131 | return false; 132 | } 133 | 134 | $post = $this->get_page_details_by_id( $post_id ); 135 | if ( ! $post ) { 136 | return false; 137 | } 138 | 139 | return get_home_url() . '/wp-content/plugins/' . $this->plugin_name . '/page/rest-routes/page.php?slug=' . $post['slug'] . '&type=' . $post['type']; 140 | } 141 | 142 | /** 143 | * Get full post data by post slug and type. 144 | * 145 | * @param string $post_slug Page Slug to do Query by. 146 | * @param string $post_type Page Type to do Query by. 147 | * @return array 148 | * 149 | * @since 1.0.0 150 | */ 151 | public function get_page_data_by_slug( $post_slug = null, $post_type = null ) { 152 | if ( ! ( $post_slug || $post_type ) ) { 153 | return false; 154 | } 155 | 156 | $page_output = ''; 157 | $args = array( 158 | 'name' => $post_slug, 159 | 'post_type' => $post_type, 160 | 'posts_per_page' => 1, 161 | 'no_found_rows' => true, 162 | ); 163 | 164 | $the_query = new \WP_Query( $args ); 165 | 166 | if ( $the_query->have_posts() ) { 167 | while ( $the_query->have_posts() ) { 168 | $the_query->the_post(); 169 | $post_id = $the_query->post->ID; 170 | $post_type = $the_query->post->post_type; 171 | $page_output = (array) $the_query->post; 172 | 173 | $featured_image = apply_filters( 'djc_set_items_featured_image', $this->get_featured_image( $post_id ) ); 174 | if ( $featured_image !== false ) { 175 | $page_output['featured_image'] = $featured_image; 176 | } 177 | 178 | $tags = apply_filters( 'djc_set_items_tags', $this->get_tags( $post_id, $post_type ) ); 179 | if ( $tags !== false ) { 180 | $page_output['tags'] = $tags; 181 | } 182 | 183 | $category = apply_filters( 'djc_set_items_category', $this->get_category( $post_id, $post_type ) ); 184 | if ( $category !== false ) { 185 | $page_output['category'] = $category; 186 | } 187 | 188 | $custom_fields = apply_filters( 'djc_set_items_custom_fields', $this->get_custom_fields( $post_id ) ); 189 | if ( $custom_fields !== false ) { 190 | $page_output['custom_fields'] = $custom_fields; 191 | } 192 | 193 | $template = apply_filters( 'djc_set_items_page_template', $this->get_page_template( $post_id, $post_type ) ); 194 | if ( $template !== false ) { 195 | $page_output['template'] = $template; 196 | } 197 | 198 | $format = apply_filters( 'djc_set_items_post_format', $this->get_post_format( $post_id ) ); 199 | if ( $format !== false ) { 200 | $page_output['format'] = $format; 201 | } 202 | 203 | // Allow developers to add new items to list. 204 | if ( has_filter( 'djc_set_items_append' ) ) { 205 | $appended_key = apply_filters( 'djc_set_items_append', $page_output ); 206 | 207 | // Must be array. 208 | if ( is_array( $appended_key ) ) { 209 | $page_output = array_merge( $page_output, $appended_key ); 210 | } 211 | } 212 | } 213 | wp_reset_postdata(); 214 | } 215 | 216 | return $page_output; 217 | } 218 | 219 | /** 220 | * Return post tags for specific post/page 221 | * 222 | * @param int $post_id Page/Post ID. 223 | * @param string $post_type Type. 224 | * @return string 225 | * 226 | * @since 1.0.0 227 | */ 228 | public function get_tags( $post_id = null, $post_type = null ) { 229 | if ( ! ( $post_id || $post_type ) ) { 230 | return; 231 | } 232 | 233 | if ( $post_type !== 'post' ) { 234 | return false; 235 | } 236 | 237 | return get_the_tags( $post_id ); 238 | } 239 | 240 | /** 241 | * Return post category for specific post/page 242 | * 243 | * @param int $post_id Page/Post ID. 244 | * @param string $post_type Type. 245 | * @return string 246 | * 247 | * @since 1.0.0 248 | */ 249 | public function get_category( $post_id = null, $post_type = null ) { 250 | if ( ! ( $post_id || $post_type ) ) { 251 | return; 252 | } 253 | 254 | if ( $post_type !== 'post' ) { 255 | return false; 256 | } 257 | 258 | return get_the_category( $post_id ); 259 | } 260 | 261 | /** 262 | * Return post featured image with all sizes for specific post/page 263 | * 264 | * @param int $post_id Page/Post ID. 265 | * @return string 266 | * 267 | * @since 1.0.0 268 | */ 269 | public function get_featured_image( $post_id = null ) { 270 | if ( ! $post_id ) { 271 | return; 272 | } 273 | 274 | // Get all registered image sizes. 275 | $image_sizes = get_intermediate_image_sizes(); 276 | 277 | $featured_image = array(); 278 | foreach ( $image_sizes as $image_size ) { 279 | $featured_image[ $image_size ] = get_the_post_thumbnail_url( $post_id, $image_size ); 280 | } 281 | 282 | return $featured_image; 283 | } 284 | 285 | /** 286 | * Return post format for specific post/page 287 | * 288 | * @param int $post_id Page/Post ID. 289 | * @return string 290 | * 291 | * @since 1.0.0 292 | */ 293 | public function get_post_format( $post_id = null ) { 294 | if ( ! $post_id ) { 295 | return; 296 | } 297 | 298 | return get_post_format( $post_id ); 299 | } 300 | 301 | /** 302 | * Return all custom fields for specific post/page 303 | * 304 | * @param int $post_id Page/Post ID. 305 | * @return array 306 | * 307 | * @since 1.0.0 308 | */ 309 | public function get_custom_fields( $post_id = null ) { 310 | if ( ! $post_id ) { 311 | return; 312 | } 313 | 314 | return get_post_custom( $post_id ); 315 | } 316 | 317 | /** 318 | * Return page template for specific post/page 319 | * 320 | * @param int $post_id Page/Post ID. 321 | * @param string $post_type Type. 322 | * @return string 323 | * 324 | * @since 1.0.0 325 | */ 326 | public function get_page_template( $post_id = null, $post_type = null ) { 327 | if ( ! ( $post_id || $post_type ) ) { 328 | return; 329 | } 330 | 331 | // Return ony for pages. 332 | if ( $post_type === 'page' ) { 333 | $template = get_page_template_slug( $post_id ); 334 | 335 | // IF template is default or no custom template is available. 336 | if ( empty( $template ) ) { 337 | return 'default'; 338 | } 339 | 340 | // Return template name without extension. 341 | return rtrim( $template, '.php' ); 342 | } 343 | 344 | return false; 345 | } 346 | 347 | /** 348 | * Return Page in JSON format 349 | * 350 | * @param string $post_slug Page Slug. 351 | * @param string $post_type Page Type. 352 | * @return json 353 | * 354 | * @since 1.0.0 355 | */ 356 | public function get_json_page( $post_slug = null, $post_type = null ) { 357 | if ( ! ( $post_slug || $post_type ) ) { 358 | return false; 359 | } 360 | 361 | return wp_json_encode( $this->get_page_data_by_slug( $post_slug, $post_type ) ); 362 | } 363 | 364 | /** 365 | * Get the array of allowed types to do operations on. 366 | * 367 | * @return array 368 | * 369 | * @since 1.0.0 370 | */ 371 | public function get_allowed_post_types() { 372 | 373 | $default = $this->default_allowed_post_types(); 374 | 375 | // Allow developers to add new items to array. 376 | if ( has_filter( 'djc_set_items_allowed_post_types' ) ) { 377 | $filtered = apply_filters( 'djc_set_items_allowed_post_types', $default ); 378 | 379 | // Must be array. 380 | if ( is_array( $filtered ) ) { 381 | $default = $filtered; 382 | } 383 | } 384 | 385 | return $default; 386 | } 387 | 388 | /** 389 | * Set default allowed post types 390 | * 391 | * @return array 392 | * 393 | * @since 1.0.0 394 | */ 395 | public function default_allowed_post_types() { 396 | return array( 'post', 'page' ); 397 | } 398 | 399 | /** 400 | * Check if post type is allowed to be save in transient. 401 | * 402 | * @param string $post_type Get post type. 403 | * @return boolean 404 | * 405 | * @since 1.0.0 406 | */ 407 | public function is_post_type_allowed_to_save( $post_type = null ) { 408 | if ( ! $post_type ) { 409 | return false; 410 | } 411 | 412 | $allowed_types = $this->get_allowed_post_types(); 413 | 414 | return in_array( $post_type, $allowed_types, true ); 415 | } 416 | 417 | /** 418 | * Update Page to transient for caching on action hooks save_post. 419 | * 420 | * @param int $post_id Saved Post ID provided by action hook. 421 | * 422 | * @since 1.0.0 423 | */ 424 | public function update_page_transient( $post_id ) { 425 | 426 | // Remove updating on save. 427 | if ( has_filter( 'djc_remove_items_updating' ) ) { 428 | $remove_updating = apply_filters( 'djc_remove_items_updating', '' ); 429 | 430 | if ( $remove_updating === true ) { 431 | return false; 432 | } 433 | } 434 | 435 | $post_status = get_post_status( $post_id ); 436 | 437 | $post = $this->get_page_details_by_id( $post_id ); 438 | if ( ! $post ) { 439 | return false; 440 | } 441 | 442 | $cache_name = $this->get_page_cache_name_by_slug( $post['slug'], $post['type'] ); 443 | if ( ! $cache_name ) { 444 | return false; 445 | } 446 | 447 | if ( $post_status === 'auto-draft' || $post_status === 'inherit' ) { 448 | return false; 449 | } elseif ( $post_status === 'trash' ) { 450 | delete_transient( $cache_name ); 451 | } else { 452 | if ( $this->is_post_type_allowed_to_save( $post['type'] ) ) { 453 | $cache = $this->get_json_page( $post['slug'], $post['type'] ); 454 | set_transient( $cache_name, $cache, 0 ); 455 | } 456 | } 457 | } 458 | 459 | /** 460 | * Set all pages in transient in case all transients are deleted. 461 | * Called on custom button from admin. 462 | * Really heavy!!! 463 | * 464 | * @since 1.0.0 465 | */ 466 | public function set_all_pages_transient() { 467 | 468 | if ( current_user_can( 'edit_users' ) ) { 469 | $allowed_types = $this->get_allowed_post_types(); 470 | 471 | $args = array( 472 | 'post_type' => $allowed_types, 473 | 'posts_per_page' => 5000, 474 | ); 475 | 476 | $the_query = new \WP_Query( $args ); 477 | 478 | if ( $the_query->have_posts() ) { 479 | while ( $the_query->have_posts() ) { 480 | $the_query->the_post(); 481 | $post_slug = $the_query->post->post_name; 482 | $post_type = $the_query->post->post_type; 483 | 484 | $cache_name = $this->get_page_cache_name_by_slug( $post_slug, $post_type ); 485 | if ( ! $cache_name ) { 486 | return false; 487 | } 488 | 489 | $cache = $this->get_json_page( $post_slug, $post_type ); 490 | 491 | set_transient( $cache_name, $cache, 0 ); 492 | } 493 | wp_reset_postdata(); 494 | } 495 | } 496 | } 497 | 498 | /** 499 | * Ajax function to rebuild all data transients 500 | * 501 | * @since 1.0.0 502 | */ 503 | public function djc_rebuild_items_transients_ajax() { 504 | $general_helper = new General_Helpers\General_Helper(); 505 | 506 | if ( ! isset( $_POST['djcRebuildNonce'] ) && ! wp_verify_nonce( sanitize_key( $_POST['djcRebuildNonce'] ), 'djc_rebuild_nonce_action' ) ) { 507 | wp_send_json( $general_helper->set_msg_array( 'error', 'Check your nonce!' ) ); 508 | } 509 | 510 | $this->set_all_pages_transient(); 511 | 512 | wp_send_json( $general_helper->set_msg_array( 'success', 'Success in rebuilding transients for cache!' ) ); 513 | } 514 | 515 | /** 516 | * Add Columns for Post/Page/Custom post type 517 | * 518 | * @param array $columns columns. 519 | * @return array 520 | * 521 | * @since 1.0.0 522 | */ 523 | public function add_admin_columns( $columns ) { 524 | $columns['cached'] = esc_html__( 'Cached', 'decoupled_json_content' ); 525 | return $columns; 526 | } 527 | 528 | /** 529 | * Return html for endpoint link depending on transient state. 530 | * 531 | * @param int $post_id post_id. 532 | * @return html 533 | */ 534 | public function get_enpoint_link( $post_id = null ) { 535 | if ( ! $post_id ) { 536 | return; 537 | } 538 | 539 | $cache_name = $this->get_page_cache_name_by_id( $post_id ); 540 | if ( ! $cache_name ) { 541 | return false; 542 | } 543 | 544 | $cache = get_transient( $cache_name ); 545 | if ( $cache === false ) { 546 | return ''; 547 | } else { 548 | return ' ' . esc_html__( 'API', 'decoupled_json_content' ) . ''; 549 | } 550 | } 551 | 552 | /** 553 | * Add Columns Content for Events Type 554 | * 555 | * @param array $column column. 556 | * @param int $post_id post_id. 557 | * 558 | * @since 1.0.0 559 | */ 560 | public function add_admin_columns_content( $column, $post_id ) { 561 | 562 | switch ( $column ) { 563 | case 'cached': 564 | echo wp_kses_post( $this->get_enpoint_link( $post_id ) ); 565 | break; 566 | } 567 | } 568 | 569 | /** 570 | * Add endpoint link to the publish meta box 571 | * 572 | * @since 1.0.0 573 | */ 574 | public function add_publish_meta_options() { 575 | global $post; 576 | 577 | if ( $this->is_post_type_allowed_to_save( $post->post_type ) ) { 578 | printf( '
%s
', wp_kses_post( $this->get_enpoint_link( $post->ID ) ) ); 579 | } 580 | } 581 | } 582 | -------------------------------------------------------------------------------- /page/rest-routes/page.php: -------------------------------------------------------------------------------- 1 | set_msg_array( 'error', 'Error, slug is missing!' ) ); 31 | } 32 | 33 | // Check post type. 34 | if ( isset( $_GET['type'] ) && ! empty( $_GET['type'] ) ) { // WPCS: input var ok; CSRF ok. 35 | $post_type = sanitize_text_field( wp_unslash( $_GET['type'] ) ); // WPCS: input var ok; CSRF ok. 36 | } else { 37 | wp_send_json( $general_helper->set_msg_array( 'error', 'Error, type is missing!' ) ); 38 | } 39 | 40 | $cache = get_transient( $page->get_page_cache_name_by_slug( $post_slug, $post_type ) ); 41 | 42 | if ( $cache === false ) { 43 | wp_send_json( $general_helper->set_msg_array( 'error', 'Error, there is a problem with your configuration or pages/posts are not cached correctly. Please check your configuration, rebuild the cache and try again!' ) ); 44 | } 45 | 46 | wp_send_json( json_decode( $cache ) ); 47 | -------------------------------------------------------------------------------- /skin/assets/application.js: -------------------------------------------------------------------------------- 1 | // Load scripts 2 | import './scripts/scripts'; 3 | -------------------------------------------------------------------------------- /skin/assets/scripts/regenerateDataTransients.js: -------------------------------------------------------------------------------- 1 | /* global djcLocalization */ 2 | 3 | export default class RegenerateDataTransients { 4 | constructor(options) { 5 | this.ajaxAction = options.ajaxAction; 6 | this.nonceField = options.nonceField; 7 | this.msgSelector = options.msgSelector; 8 | 9 | this.$nonceField = $(this.nonceField); 10 | this.$msg = $(this.msgSelector); 11 | } 12 | 13 | rebuild(filter, type) { 14 | let actionFilter = ''; 15 | 16 | if (typeof filter !== 'undefined') { 17 | actionFilter = filter; 18 | } 19 | 20 | let postType = ''; 21 | 22 | if (typeof type !== 'undefined') { 23 | postType = type; 24 | } 25 | 26 | const data = { 27 | action: this.ajaxAction, 28 | djcRebuildNonce: this.$nonceField.val(), 29 | actionFilter, 30 | postType, 31 | }; 32 | 33 | $.post(djcLocalization.ajaxurl, data, (response) => { 34 | this.setMsg(response); 35 | }, 'json'); 36 | } 37 | 38 | setMsg(data) { 39 | if (typeof data === 'undefined') { 40 | return false; 41 | } 42 | 43 | this.$msg.html(`

${data.msg}

`); 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /skin/assets/scripts/scripts.js: -------------------------------------------------------------------------------- 1 | /* global djcLocalization */ 2 | import RegenerateDataTransients from './regenerateDataTransients'; 3 | 4 | $(function() { 5 | 6 | // Rebuild thumbnail ajax action 7 | const regenerateDataTransients = new RegenerateDataTransients({ 8 | ajaxAction: 'djc_rebuild_items_transients_ajax', 9 | nonceField: '#djc_rebuild_items_nonce', 10 | msgSelector: '.js-djc-msg', 11 | }); 12 | 13 | $('.js-djc-rebuild').on('click', (event) => { 14 | event.preventDefault(); 15 | 16 | const confirmAction = confirm(djcLocalization.confirmRebuildAction); // eslint-disable-line no-alert 17 | if (confirmAction) { 18 | regenerateDataTransients.rebuild(); 19 | } 20 | }); 21 | 22 | // Rebuild thumbnail ajax action 23 | const regenerateDataListTransients = new RegenerateDataTransients({ 24 | ajaxAction: 'djc_rebuild_lists_transients_ajax', 25 | nonceField: '#djc_rebuild_lists_nonce', 26 | msgSelector: '.js-djc-msg', 27 | }); 28 | 29 | $('.js-djc-rebuild-data-list').on('click', (event) => { 30 | event.preventDefault(); 31 | 32 | const confirmAction = confirm(djcLocalization.confirmRebuildAction); // eslint-disable-line no-alert 33 | if (confirmAction) { 34 | const actionFilter = $(event.target).attr('data-action-filter'); 35 | const postType = $(event.target).attr('data-post-type'); 36 | regenerateDataListTransients.rebuild(actionFilter, postType); 37 | } 38 | }); 39 | 40 | }); 41 | -------------------------------------------------------------------------------- /uninstall.php: -------------------------------------------------------------------------------- 1 |