├── .gitignore ├── LICENSE ├── README.md ├── _build ├── build.config.php ├── config.json ├── data │ └── .gitignore ├── gpm_resolvers │ └── gpm.resolve.tv_templates.php ├── includes │ └── .gitignore ├── resolvers │ └── .gitignore └── setup.options.php ├── _packages └── ampify-0.2.1-dev1.transport.zip ├── config.core.php └── core └── components └── ampify ├── docs ├── changelog.txt ├── license.txt └── readme.txt ├── elements ├── .gitignore ├── plugins │ └── ampify.plugin.php ├── snippets │ └── ampfilter.snippet.php └── templates │ └── amp_template.template.tpl ├── index.php └── model ├── ampify └── ampify.class.php └── modx-amp ├── class-amp-content.php └── includes ├── lib └── class-fastimage.php ├── sanitizers ├── class-amp-audio-sanitizer.php ├── class-amp-base-sanitizer.php ├── class-amp-blacklist-sanitizer.php ├── class-amp-iframe-sanitizer.php ├── class-amp-img-sanitizer.php └── class-amp-video-sanitizer.php └── utils ├── class-amp-dom-utils.php └── class-amp-html-utils.php /.gitignore: -------------------------------------------------------------------------------- 1 | _build/build.config.php 2 | config.core.php 3 | .idea 4 | .settings 5 | .project 6 | _packages/*.* 7 | !_packages/.gitignore 8 | !_packages/*.zip -------------------------------------------------------------------------------- /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 | {description} 294 | Copyright (C) {year} {fullname} 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 | {signature of Ty Coon}, 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 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ampify 2 | 3 | Effortless Google [Accelerated Mobile Pages](https://www.ampproject.org/), aka "AMP", for MODX Revolution. 4 | 5 | ## What is it? 6 | 7 | **Ampify** is a MODX Revolution Extra that includes a toolset to easily serve AMP HTML for Google's search results pages. The package includes a Plugin, and a sample AMP Template. 8 | 9 | ## What does it do? 10 | 11 | When triggered, the Plugin switches the Template used by the currently requested Resource, to the specified AMP Template. 12 | 13 | ## I want details 14 | 15 | The Ampify Plugin fires in two different modes: 'param' and 'context'. 16 | 17 | ### Context Mode 18 | 19 | In 'context' mode, the Plugin action is triggered when a request is made to a specified AMP Context. (Context routing and setup are outside the scope of this Extra, but there's an [example implementation](#example-implementation) below.) 20 | 21 | The AMP Context will automatically route requests to Resources that have been added to the ContextResource table. Resources are added automatically when they are saved. In future versions, a CMP will be provided to add/remove Resources from the table, with bulk actions. 22 | 23 | This mode has the advantage of being able to include and exclude Resources from having an AMP view. 24 | 25 | However, it has the disadvantage that AMP views will be rendered at a different URI than the canonical. At the minimum, the Context's base_url will be prefixed, for example: `/amp/resource-alias.html` versus `/resource-alias.html` 26 | 27 | Also, if you have a lot of pre-existing Resources for which you want to provide an AMP view, you'll need to add them all to the ContextResource table by triggering the `OnDocFormSave` Event on all of them—or modify the ContextResource table yourself, programmatically. Until the CMP with bulk actions is developed (in a later version soon-to-come) this is a drawback of the 'context' mode. 28 | 29 | ### Param Mode 30 | 31 | In 'param' mode, the Plugin action is triggered when the GET param specified is set, in the URL string. This will override the `amp_context` property. In this mode: 32 | 33 | - If a request does not have the GET param specified, the Plugin will not fire, even if the request is in the specified `amp_context`. 34 | - The Plugin action will not be triggered on the `OnDocFormSave` Event, and the ContextResource table will not be modified while in this mode. 35 | 36 | This mode has the advantage of rendering the AMP view at the same URI as the requested Resource. Nothing about the Resource's URI needs to change, except the presence of the specified GET param. 37 | 38 | Also, the AMP view becomes immediately enabled for all your Resources, without any additional modifications of database records. 39 | 40 | However, it has the disadvantage that Resources cannot be specifically included or excluded from having an AMP view—the Plugin will fire for all Resources if the GET param is present. 41 | 42 | Another, perhaps more critical drawback, is that **the AMP view will not be cacheable**. This can cause serious performance issues, depending on the complexity of the AMP Template used. It's true that Google caches the AMP views, but it opens a door for anyone to trigger high loads on your site, if your AMP Template requires processing. 43 | 44 | A workaround for excluding Resources from the AMP view, would be to use the `amp_tv` property, and for Resources you want to exclude from having an AMP view, set the TV value to the same Template that the Resource uses natively. However, if the URL param is set, **the Resource will not be cacheable**. There's no workaround in this version of Ampify, for the caching limitation in 'param' mode. 45 | 46 | ### AMP TV 47 | 48 | If a value is provided in the `amp_tv` Plugin property, then the Resource will _only_ be added to the ContextResource table, if the Resource has a valid Template ID set in the TV. In that case, it will override the Template ID in the Plugin's `amp_template` property `OnLoadWebDocument` _for that Resource_. 49 | 50 | If the TV is empty for a given Resource, that Resource will be removed from the ContextResource table. 51 | 52 | In 'param' mode, no modifications happen to the ContextResource table, but the `amp_tv` value will be used to render the AMP view, falling back to the `amp_template` Plugin property as default. 53 | 54 | ## Installation 55 | 56 | Install via the MODX Revolution Extras Installer (aka "Package Manager"). 57 | 58 | ## Usage 59 | 60 | The default Plugin property values are: 61 | 62 | - `amp_context` = `""` If this property is empty or doesn't contain a valid Context key, the Plugin will try to run in 'param' mode. 63 | - `amp_url_param` = `""` If this property is set, it will override the `amp_context` property and trigger the AMP Template view if a GET param with the specified key is set in the URL string. If this property is empty and `amp_context` is also empty, the Plugin effectively does nothing. 64 | - `amp_template` = `""` This property is required. At least 1 Template must be dedicated to an AMP view, and that Template's ID set here, in order for the Plugin to work at all. 65 | - `amp_tv` = `""` If a valid TV **name** is entered here, it will transform the Plugin's actions in the following way: 66 | - `OnLoadWebDocument` it will switch the Template of the Resource to the one specified in the TV with name `amp_tv`, falling back to the default set in `amp_template` 67 | - `OnDocFormSave` it will _only_ add the Resource to the table for automatic routing if there's a truth-y value in the TV with name `amp_tv`. If the TV is empty or false-y, it will remove the Resource from the table. This provides primitive "remove" functionality until such time a CMP is made to manage ContextResources. 68 | 69 | ## Example Implementation 70 | 71 | In this example, we'll install Ampify into an almost fresh MODX install, and use [Context Gateway](https://github.com/modxcms/context-gateway/) for Context routing. Both can be installed via the Extras Installer: 72 | 73 | ![Extras Installer](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.34.55.png) 74 | 75 | ### Create a new Context. 76 | 77 | Hover over the cog icon in the top-right corner of the Manager, to reveal the "Contexts" menu item. 78 | 79 | ![Contexts menu item](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.35.02.png) 80 | 81 | In the Contexts view, click "Create New". 82 | 83 | ![Create Context button](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.35.13.png) 84 | 85 | Give the new Context a key, a user-friendly name, and optionally a description. 86 | 87 | ![Create Context window](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.35.24.png) 88 | 89 | Once the Context is created, you'll see it in the grid-list of Contexts. Right-click on it and select "Update Context". 90 | 91 | ![Update Context](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2008.34.43.png) 92 | 93 | In the Context Edit view, select the tab "Context Settings". 94 | 95 | ![Context Settings tab](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2008.35.20.png) 96 | 97 | If you're not familiar with Context Settings, here are the documentation pages for [Contexts](https://rtfm.modx.com/revolution/2.x/administering-your-site/contexts) and [System Settings](https://rtfm.modx.com/revolution/2.x/administering-your-site/settings), which you might find useful to review before moving on to the next step. 98 | 99 | Click the "Create New" button. You'll need to create 5 new Context Settings, at a minimum: 100 | 101 | 1. `ctx_alias` is a requirement of the Context Gateway router, and defines the URI-bit for this Context. You'll use this string again in other settings. 102 | 103 | ![ctx_alias setting](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.37.42.png) 104 | 105 | 2. `site_url` overrides the default System Setting, appending the `ctx_alias`. 106 | 107 | ![site_url Context setting](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.37.26.png) 108 | 109 | Notice the syntax `{site_url}amp/`. The part in curly braces references the default MODX `site_url` dynamically. 110 | 111 | 3. `base_url` does essentially the same as `site_url` but for a specific component of the URL. 112 | 113 | ![base_url setting](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.37.34.png) 114 | 115 | 4. `site_start` and `error_page` should both have, as their values, the ID of the Resource that MODX will render as the "homepage" of the new Context. If you haven't created a Resource in the new Context yet, you can do so without leaving the current view, by right-clicking on the Context node in the Resource Tree, and selecting the "Quick Create" » "Document" option: 116 | 117 | ![Quick Create](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2008.44.35.png) 118 | 119 | Give the new Resource a title, and ensure it's published. Once you hit save, the Tree will refresh and you'll see the new Resource with it's ID in parenthesis: 120 | 121 | ![Resource Tree](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2008.47.02.png) 122 | 123 | ### Friendly URL System Setting 124 | 125 | If you haven't already enabled Friendly URLs for your site, you'll need to do so for Ampify and Context Gateway to work. Hover over the cog icon again and chooose "System Settings". 126 | 127 | ![System Settings menu item](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2008.49.39.png) 128 | 129 | At the top of the grid, choose the "Friendly URL" option from the "Area" dropdown: 130 | 131 | ![Area dropdown](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.39.08.png) 132 | 133 | Locate the "Use Friendly URLs" setting and set it to "Yes". 134 | 135 | ![FURLs](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.39.17.png) 136 | 137 | ### Plugin Properties 138 | 139 | Next you'll setup the AMPIFY Plugin's properties, without which the Plugin doesn't fire. You'll need the ID of the AMP Template that you want to use. Ampify installs a sample Template. 140 | 141 | ![sample AMP Template](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.40.10.png) 142 | 143 | In this installation the ID is `4`. In the "Elements" tab of the Tree, open the "Plugins" section if not already open, and locate the AMPIFY Plugin: 144 | 145 | ![Elements Tree](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2008.57.17.png) 146 | 147 | ![AMPIFY Plugin edit](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.40.16.png) 148 | 149 | In the Plugin Edit view, select the "Properties" tab: 150 | 151 | ![Plugin properties](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.41.06.png) 152 | 153 | Click the button "Add Property Set". It's recommended to use a custom property set that will not be overwritten when you update the Extra. 154 | 155 | ![Add Property Set](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2008.57.54.png) 156 | 157 | In the "Add Property Set" window, select "Create New Property Set" and give it a name: 158 | 159 | ![Add Property Set Window](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.41.32.png) 160 | 161 | Hit save. In the property set dropdown, select your new set: 162 | 163 | ![Dropdown](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-24%2013.41.41.png) 164 | 165 | Modify the property values, as per your desired configuration. In this implementation, we'll add the Context Key and Template ID: 166 | 167 | ![Property set for AMP](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2009.00.07.png) 168 | 169 | Save the property set. 170 | 171 | ![Save property set](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2009.17.28.png) 172 | 173 | You'll also need to select the custom property set for the Plugin's Event triggers. Go to the "System Events" tab, and select the custom property set for both enabled Events: 174 | 175 | ![Plugin Events](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2009.19.06.png) 176 | 177 | Don't forget to "Save" the Plugin! 178 | 179 | ![Save Plugin](https://www.sepiariver.ca/assets/uploads/images/Screenshot%202016-08-25%2009.19.22.png) 180 | 181 | Now the Plugin should be running in 'context' mode. When you save a Resource, it will be added to the `ContextResource` table for the `amp` Context. The AMP view for the Resource will be a the Resource's URI, prefixed with the `ctx_alias`: `amp/`. 182 | -------------------------------------------------------------------------------- /_build/build.config.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/_build/build.config.php -------------------------------------------------------------------------------- /_build/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "AMPIFY" 3 | ,"lowCaseName": "ampify" 4 | ,"description": "Ampify your MODX Resources." 5 | ,"author": "YJ Tso, Wayne Roddy" 6 | ,"version": "0.2.2-dev1" 7 | ,"package":{ 8 | "elements": { 9 | "plugins": [{ 10 | "name": "AMPIFY" 11 | ,"file": "ampify.plugin.php" 12 | ,"events": ["OnLoadWebDocument","OnDocFormSave"] 13 | ,"properties": [{ 14 | "name": "amp_context" 15 | ,"description": "Key of the Context in which to render AMP pages" 16 | ,"value": "" 17 | },{ 18 | "name": "amp_template" 19 | ,"description": "ID of Template with AMP markup" 20 | },{ 21 | "name": "amp_tv" 22 | ,"description": "Optional: NAME of TV specifying AMP Template for each Resource e.g. amp_template" 23 | },{ 24 | "name": "amp_url_param" 25 | ,"description": "Key of GET param to trigger AMP Template rendering. Overrides amp_context if set." 26 | }] 27 | }] 28 | ,"snippets": [{ 29 | "name": "ampfilter", 30 | "file": "ampfilter.snippet.php", 31 | "description": "Output modifier to convert HTML to amp-compliant markup." 32 | }] 33 | ,"templates": [{ 34 | "name": "Sample AMP Template" 35 | ,"file": "amp_template.template.tpl" 36 | }] 37 | ,"tvs": [{ 38 | "name": "amp_template" 39 | ,"caption": "AMP Template ID" 40 | ,"description": "Select the AMP Template to be used for this Resource" 41 | ,"type": "listbox" 42 | ,"inputOptionValues": "@SELECT templatename, id FROM modx_site_templates" 43 | }] 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /_build/data/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/_build/data/.gitignore -------------------------------------------------------------------------------- /_build/gpm_resolvers/gpm.resolve.tv_templates.php: -------------------------------------------------------------------------------- 1 | xpdo) { 12 | $modx =& $object->xpdo; 13 | switch ($options[xPDOTransport::PACKAGE_ACTION]) { 14 | case xPDOTransport::ACTION_INSTALL: 15 | case xPDOTransport::ACTION_UPGRADE: 16 | /* list of tvs and templates for each */ 17 | $tvs = array ( 18 | 'amp_template' => 19 | array ( 20 | ), 21 | ); 22 | foreach ($tvs as $tvName => $templateNames) { 23 | if (!is_array($templateNames) || empty($templateNames)) continue; 24 | $tv = $modx->getObject('modTemplateVar',array('name' => $tvName)); 25 | if (empty($tv)) { 26 | $modx->log(xPDO::LOG_LEVEL_ERROR,'Could not find TV: '.$tvName.' to associate to Templates.'); 27 | continue; 28 | } 29 | $rank = 0; 30 | foreach ($templateNames as $templateName) { 31 | $template = $modx->getObject('modTemplate',array('templatename' => $templateName)); 32 | if (!empty($template)) { 33 | $templateVarTemplate = $modx->getObject('modTemplateVarTemplate',array( 34 | 'tmplvarid' => $tv->get('id'), 35 | 'templateid' => $template->get('id'), 36 | )); 37 | if (!$templateVarTemplate) { 38 | $templateVarTemplate = $modx->newObject('modTemplateVarTemplate'); 39 | $templateVarTemplate->fromArray(array( 40 | 'tmplvarid' => $tv->get('id'), 41 | 'templateid' => $template->get('id'), 42 | 'rank' => $rank, 43 | ),'',true,true); 44 | if ($templateVarTemplate->save() == false) { 45 | $modx->log(xPDO::LOG_LEVEL_ERROR,'An unknown error occurred while trying to associate the TV '.$tvName.' to the Template '.$templateName); 46 | } 47 | } 48 | } else { 49 | $modx->log(xPDO::LOG_LEVEL_ERROR,'Could not find Template '.$templateName); 50 | } 51 | $rank++; 52 | } 53 | } 54 | break; 55 | } 56 | } 57 | return true; -------------------------------------------------------------------------------- /_build/includes/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/_build/includes/.gitignore -------------------------------------------------------------------------------- /_build/resolvers/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/_build/resolvers/.gitignore -------------------------------------------------------------------------------- /_build/setup.options.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/_build/setup.options.php -------------------------------------------------------------------------------- /_packages/ampify-0.2.1-dev1.transport.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/_packages/ampify-0.2.1-dev1.transport.zip -------------------------------------------------------------------------------- /config.core.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/config.core.php -------------------------------------------------------------------------------- /core/components/ampify/docs/changelog.txt: -------------------------------------------------------------------------------- 1 | AMPIFY 0.0.1 2 | ============ 3 | Initial dev version. -------------------------------------------------------------------------------- /core/components/ampify/docs/license.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE Version 2, June 1991 2 | 3 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 4 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The licenses for most software are designed to take away your 11 | freedom to share and change it. By contrast, the GNU General Public 12 | License is intended to guarantee your freedom to share and change free 13 | software--to make sure the software is free for all its users. This 14 | General Public License applies to most of the Free Software 15 | Foundation's software and to any other program whose authors commit to 16 | using it. (Some other Free Software Foundation software is covered by 17 | the GNU Lesser General Public License instead.) You can apply it to 18 | your programs, too. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | this service if you wish), that you receive source code or can get it 24 | if you want it, that you can change the software or use pieces of it 25 | in new free programs; and that you know you can do these things. 26 | 27 | To protect your rights, we need to make restrictions that forbid 28 | anyone to deny you these rights or to ask you to surrender the rights. 29 | These restrictions translate to certain responsibilities for you if you 30 | distribute copies of the software, or if you modify it. 31 | 32 | For example, if you distribute copies of such a program, whether 33 | gratis or for a fee, you must give the recipients all the rights that 34 | you have. You must make sure that they, too, receive or can get the 35 | source code. And you must show them these terms so they know their 36 | rights. 37 | 38 | We protect your rights with two steps: (1) copyright the software, and 39 | (2) offer you this license which gives you legal permission to copy, 40 | distribute and/or modify the software. 41 | 42 | Also, for each author's protection and ours, we want to make certain 43 | that everyone understands that there is no warranty for this free 44 | software. If the software is modified by someone else and passed on, we 45 | want its recipients to know that what they have is not the original, so 46 | that any problems introduced by others will not reflect on the original 47 | authors' reputations. 48 | 49 | Finally, any free program is threatened constantly by software 50 | patents. We wish to avoid the danger that redistributors of a free 51 | program will individually obtain patent licenses, in effect making the 52 | program proprietary. To prevent this, we have made it clear that any 53 | patent must be licensed for everyone's free use or not licensed at all. 54 | 55 | The precise terms and conditions for copying, distribution and 56 | modification follow. 57 | 58 | GNU GENERAL PUBLIC LICENSE 59 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 60 | 61 | 0. This License applies to any program or other work which contains 62 | a notice placed by the copyright holder saying it may be distributed 63 | under the terms of this General Public License. The "Program", below, 64 | refers to any such program or work, and a "work based on the Program" 65 | means either the Program or any derivative work under copyright law: 66 | that is to say, a work containing the Program or a portion of it, 67 | either verbatim or with modifications and/or translated into another 68 | language. (Hereinafter, translation is included without limitation in 69 | the term "modification".) Each licensee is addressed as "you". 70 | 71 | Activities other than copying, distribution and modification are not 72 | covered by this License; they are outside its scope. The act of 73 | running the Program is not restricted, and the output from the Program 74 | is covered only if its contents constitute a work based on the 75 | Program (independent of having been made by running the Program). 76 | Whether that is true depends on what the Program does. 77 | 78 | 1. You may copy and distribute verbatim copies of the Program's 79 | source code as you receive it, in any medium, provided that you 80 | conspicuously and appropriately publish on each copy an appropriate 81 | copyright notice and disclaimer of warranty; keep intact all the 82 | notices that refer to this License and to the absence of any warranty; 83 | and give any other recipients of the Program a copy of this License 84 | along with the Program. 85 | 86 | You may charge a fee for the physical act of transferring a copy, and 87 | you may at your option offer warranty protection in exchange for a fee. 88 | 89 | 2. You may modify your copy or copies of the Program or any portion 90 | of it, thus forming a work based on the Program, and copy and 91 | distribute such modifications or work under the terms of Section 1 92 | above, provided that you also meet all of these conditions: 93 | 94 | a) You must cause the modified files to carry prominent notices 95 | stating that you changed the files and the date of any change. 96 | 97 | b) You must cause any work that you distribute or publish, that in 98 | whole or in part contains or is derived from the Program or any 99 | part thereof, to be licensed as a whole at no charge to all third 100 | parties under the terms of this License. 101 | 102 | c) If the modified program normally reads commands interactively 103 | when run, you must cause it, when started running for such 104 | interactive use in the most ordinary way, to print or display an 105 | announcement including an appropriate copyright notice and a 106 | notice that there is no warranty (or else, saying that you provide 107 | a warranty) and that users may redistribute the program under 108 | these conditions, and telling the user how to view a copy of this 109 | License. (Exception: if the Program itself is interactive but 110 | does not normally print such an announcement, your work based on 111 | the Program is not required to print an announcement.) 112 | 113 | These requirements apply to the modified work as a whole. If 114 | identifiable sections of that work are not derived from the Program, 115 | and can be reasonably considered independent and separate works in 116 | themselves, then this License, and its terms, do not apply to those 117 | sections when you distribute them as separate works. But when you 118 | distribute the same sections as part of a whole which is a work based 119 | on the Program, the distribution of the whole must be on the terms of 120 | this License, whose permissions for other licensees extend to the 121 | entire whole, and thus to each and every part regardless of who wrote it. 122 | 123 | Thus, it is not the intent of this section to claim rights or contest 124 | your rights to work written entirely by you; rather, the intent is to 125 | exercise the right to control the distribution of derivative or 126 | collective works based on the Program. 127 | 128 | In addition, mere aggregation of another work not based on the Program 129 | with the Program (or with a work based on the Program) on a volume of 130 | a storage or distribution medium does not bring the other work under 131 | the scope of this License. 132 | 133 | 3. You may copy and distribute the Program (or a work based on it, 134 | under Section 2) in object code or executable form under the terms of 135 | Sections 1 and 2 above provided that you also do one of the following: 136 | 137 | a) Accompany it with the complete corresponding machine-readable 138 | source code, which must be distributed under the terms of Sections 139 | 1 and 2 above on a medium customarily used for software interchange; or, 140 | 141 | b) Accompany it with a written offer, valid for at least three 142 | years, to give any third party, for a charge no more than your 143 | cost of physically performing source distribution, a complete 144 | machine-readable copy of the corresponding source code, to be 145 | distributed under the terms of Sections 1 and 2 above on a medium 146 | customarily used for software interchange; or, 147 | 148 | c) Accompany it with the information you received as to the offer 149 | to distribute corresponding source code. (This alternative is 150 | allowed only for noncommercial distribution and only if you 151 | received the program in object code or executable form with such 152 | an offer, in accord with Subsection b above.) 153 | 154 | The source code for a work means the preferred form of the work for 155 | making modifications to it. For an executable work, complete source 156 | code means all the source code for all modules it contains, plus any 157 | associated interface definition files, plus the scripts used to 158 | control compilation and installation of the executable. However, as a 159 | special exception, the source code distributed need not include 160 | anything that is normally distributed (in either source or binary 161 | form) with the major components (compiler, kernel, and so on) of the 162 | operating system on which the executable runs, unless that component 163 | itself accompanies the executable. 164 | 165 | If distribution of executable or object code is made by offering 166 | access to copy from a designated place, then offering equivalent 167 | access to copy the source code from the same place counts as 168 | distribution of the source code, even though third parties are not 169 | compelled to copy the source along with the object code. 170 | 171 | 4. You may not copy, modify, sublicense, or distribute the Program 172 | except as expressly provided under this License. Any attempt 173 | otherwise to copy, modify, sublicense or distribute the Program is 174 | void, and will automatically terminate your rights under this License. 175 | However, parties who have received copies, or rights, from you under 176 | this License will not have their licenses terminated so long as such 177 | parties remain in full compliance. 178 | 179 | 5. You are not required to accept this License, since you have not 180 | signed it. However, nothing else grants you permission to modify or 181 | distribute the Program or its derivative works. These actions are 182 | prohibited by law if you do not accept this License. Therefore, by 183 | modifying or distributing the Program (or any work based on the 184 | Program), you indicate your acceptance of this License to do so, and 185 | all its terms and conditions for copying, distributing or modifying 186 | the Program or works based on it. 187 | 188 | 6. Each time you redistribute the Program (or any work based on the 189 | Program), the recipient automatically receives a license from the 190 | original licensor to copy, distribute or modify the Program subject to 191 | these terms and conditions. You may not impose any further 192 | restrictions on the recipients' exercise of the rights granted herein. 193 | You are not responsible for enforcing compliance by third parties to 194 | this License. 195 | 196 | 7. If, as a consequence of a court judgment or allegation of patent 197 | infringement or for any other reason (not limited to patent issues), 198 | conditions are imposed on you (whether by court order, agreement or 199 | otherwise) that contradict the conditions of this License, they do not 200 | excuse you from the conditions of this License. If you cannot 201 | distribute so as to satisfy simultaneously your obligations under this 202 | License and any other pertinent obligations, then as a consequence you 203 | may not distribute the Program at all. For example, if a patent 204 | license would not permit royalty-free redistribution of the Program by 205 | all those who receive copies directly or indirectly through you, then 206 | the only way you could satisfy both it and this License would be to 207 | refrain entirely from distribution of the Program. 208 | 209 | If any portion of this section is held invalid or unenforceable under 210 | any particular circumstance, the balance of the section is intended to 211 | apply and the section as a whole is intended to apply in other 212 | circumstances. 213 | 214 | It is not the purpose of this section to induce you to infringe any 215 | patents or other property right claims or to contest validity of any 216 | such claims; this section has the sole purpose of protecting the 217 | integrity of the free software distribution system, which is 218 | implemented by public license practices. Many people have made 219 | generous contributions to the wide range of software distributed 220 | through that system in reliance on consistent application of that 221 | system; it is up to the author/donor to decide if he or she is willing 222 | to distribute software through any other system and a licensee cannot 223 | impose that choice. 224 | 225 | This section is intended to make thoroughly clear what is believed to 226 | be a consequence of the rest of this License. 227 | 228 | 8. If the distribution and/or use of the Program is restricted in 229 | certain countries either by patents or by copyrighted interfaces, the 230 | original copyright holder who places the Program under this License 231 | may add an explicit geographical distribution limitation excluding 232 | those countries, so that distribution is permitted only in or among 233 | countries not thus excluded. In such case, this License incorporates 234 | the limitation as if written in the body of this License. 235 | 236 | 9. The Free Software Foundation may publish revised and/or new versions 237 | of the General Public License from time to time. Such new versions will 238 | be similar in spirit to the present version, but may differ in detail to 239 | address new problems or concerns. 240 | 241 | Each version is given a distinguishing version number. If the Program 242 | specifies a version number of this License which applies to it and "any 243 | later version", you have the option of following the terms and conditions 244 | either of that version or of any later version published by the Free 245 | Software Foundation. If the Program does not specify a version number of 246 | this License, you may choose any version ever published by the Free Software 247 | Foundation. 248 | 249 | 10. If you wish to incorporate parts of the Program into other free 250 | programs whose distribution conditions are different, write to the author 251 | to ask for permission. For software which is copyrighted by the Free 252 | Software Foundation, write to the Free Software Foundation; we sometimes 253 | make exceptions for this. Our decision will be guided by the two goals 254 | of preserving the free status of all derivatives of our free software and 255 | of promoting the sharing and reuse of software generally. 256 | 257 | NO WARRANTY 258 | 259 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 260 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 261 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 262 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 263 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 264 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 265 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 266 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 267 | REPAIR OR CORRECTION. 268 | 269 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 270 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 271 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 272 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 273 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 274 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 275 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 276 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 277 | POSSIBILITY OF SUCH DAMAGES. 278 | 279 | END OF TERMS AND CONDITIONS 280 | 281 | How to Apply These Terms to Your New Programs 282 | 283 | If you develop a new program, and you want it to be of the greatest 284 | possible use to the public, the best way to achieve this is to make it 285 | free software which everyone can redistribute and change under these terms. 286 | 287 | To do so, attach the following notices to the program. It is safest 288 | to attach them to the start of each source file to most effectively 289 | convey the exclusion of warranty; and each file should have at least 290 | the "copyright" line and a pointer to where the full notice is found. 291 | 292 | {description} 293 | Copyright (C) {year} {fullname} 294 | 295 | This program is free software; you can redistribute it and/or modify 296 | it under the terms of the GNU General Public License as published by 297 | the Free Software Foundation; either version 2 of the License, or 298 | (at your option) any later version. 299 | 300 | This program is distributed in the hope that it will be useful, 301 | but WITHOUT ANY WARRANTY; without even the implied warranty of 302 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 303 | GNU General Public License for more details. 304 | 305 | You should have received a copy of the GNU General Public License along 306 | with this program; if not, write to the Free Software Foundation, Inc., 307 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 308 | 309 | Also add information on how to contact you by electronic and paper mail. 310 | 311 | If the program is interactive, make it output a short notice like this 312 | when it starts in an interactive mode: 313 | 314 | Gnomovision version 69, Copyright (C) year name of author 315 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 316 | This is free software, and you are welcome to redistribute it 317 | under certain conditions; type `show c' for details. 318 | 319 | The hypothetical commands `show w' and `show c' should show the appropriate 320 | parts of the General Public License. Of course, the commands you use may 321 | be called something other than `show w' and `show c'; they could even be 322 | mouse-clicks or menu items--whatever suits your program. 323 | 324 | You should also get your employer (if you work as a programmer) or your 325 | school, if any, to sign a "copyright disclaimer" for the program, if 326 | necessary. Here is a sample; alter the names: 327 | 328 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 329 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 330 | 331 | {signature of Ty Coon}, 1 April 1989 332 | Ty Coon, President of Vice 333 | 334 | This General Public License does not permit incorporating your program into 335 | proprietary programs. If your program is a subroutine library, you may 336 | consider it more useful to permit linking proprietary applications with the 337 | library. If this is what you want to do, use the GNU Lesser General 338 | Public License instead of this License. 339 | 340 | -------------------------------------------------------------------------------- /core/components/ampify/docs/readme.txt: -------------------------------------------------------------------------------- 1 | # Ampify 2 | 3 | Effortless Google [Accelerated Mobile Pages](https://www.ampproject.org/), aka "AMP", for MODX Revolution. 4 | 5 | ## What is it? 6 | 7 | **Ampify** is a MODX Revolution Extra that includes a toolset to easily serve AMP HTML for Google's search results pages. The package includes a Plugin, and a sample AMP Template. 8 | 9 | ## What does it do? 10 | 11 | When triggered, the Plugin switches the Template used by the currently requested Resource, to the specified AMP Template. 12 | 13 | ## I want details 14 | 15 | The Ampify Plugin fires in two different modes: 'param' and 'context'. 16 | 17 | ### Context Mode 18 | 19 | In 'context' mode, the Plugin action is triggered when a request is made to a specified AMP Context. (Context routing and setup are outside the scope of this Extra, but there's an [example implementation](#example-implementation) below.) 20 | 21 | The AMP Context will automatically route requests to Resources that have been added to the ContextResource table. Resources are added automatically when they are saved. In future versions, a CMP will be provided to add/remove Resources from the table, with bulk actions. 22 | 23 | This mode has the advantage of being able to include and exclude Resources from having an AMP view. 24 | 25 | However, it has the disadvantage that AMP views will be rendered at a different URI than the canonical. At the minimum, the Context's base_url will be prefixed, for example: `/amp/resource-alias.html` versus `/resource-alias.html` 26 | 27 | Also, if you have a lot of pre-existing Resources for which you want to provide an AMP view, you'll need to add them all to the ContextResource table by triggering the `OnDocFormSave` Event on all of them—or modify the ContextResource table yourself, programmatically. Until the CMP with bulk actions is developed (in a later version soon-to-come) this is a drawback of the 'context' mode. 28 | 29 | ### Param Mode 30 | 31 | In 'param' mode, the Plugin action is triggered when the GET param specified is set, in the URL string. This will override the `amp_context` property. In this mode: 32 | 33 | - If a request does not have the GET param specified, the Plugin will not fire, even if the request is in the specified `amp_context`. 34 | - The Plugin action will not be triggered on the `OnDocFormSave` Event, and the ContextResource table will not be modified while in this mode. 35 | 36 | This mode has the advantage of rendering the AMP view at the same URI as the requested Resource. Nothing about the Resource's URI needs to change, except the presence of the specified GET param. 37 | 38 | Also, the AMP view becomes immediately enabled for all your Resources, without any additional modifications of database records. 39 | 40 | However, it has the disadvantage that Resources cannot be specifically included or excluded from having an AMP view—the Plugin will fire for all Resources if the GET param is present. 41 | 42 | Another, perhaps more critical drawback, is that **the AMP view will not be cacheable**. This can cause serious performance issues, depending on the complexity of the AMP Template used. It's true that Google caches the AMP views, but it opens a door for anyone to trigger high loads on your site, if your AMP Template requires processing. 43 | 44 | A workaround for excluding Resources from the AMP view, would be to use the `amp_tv` property, and for Resources you want to exclude from having an AMP view, set the TV value to the same Template that the Resource uses natively. However, if the URL param is set, **the Resource will not be cacheable**. There's no workaround in this version of Ampify, for the caching limitation in 'param' mode. 45 | 46 | ### AMP TV 47 | 48 | If a value is provided in the `amp_tv` Plugin property, then the Resource will _only_ be added to the ContextResource table, if the Resource has a valid Template ID set in the TV. In that case, it will override the Template ID in the Plugin's `amp_template` property `OnLoadWebDocument` _for that Resource_. 49 | 50 | If the TV is empty for a given Resource, that Resource will be removed from the ContextResource table. 51 | 52 | In 'param' mode, no modifications happen to the ContextResource table, but the `amp_tv` value will be used to render the AMP view, falling back to the `amp_template` Plugin property as default. 53 | 54 | ## Installation 55 | 56 | Install via the MODX Revolution Extras Installer (aka "Package Manager"). 57 | 58 | ## Usage 59 | 60 | The default Plugin property values are: 61 | 62 | - `amp_context` = `""` If this property is empty or doesn't contain a valid Context key, the Plugin will try to run in 'param' mode. 63 | - `amp_url_param` = `""` If this property is set, it will override the `amp_context` property and trigger the AMP Template view if a GET param with the specified key is set in the URL string. If this property is empty and `amp_context` is also empty, the Plugin effectively does nothing. 64 | - `amp_template` = `""` This property is required. At least 1 Template must be dedicated to an AMP view, and that Template's ID set here, in order for the Plugin to work at all. 65 | - `amp_tv` = `""` If a valid TV **name** is entered here, it will transform the Plugin's actions in the following way: 66 | - `OnLoadWebDocument` it will switch the Template of the Resource to the one specified in the TV with name `amp_tv`, falling back to the default set in `amp_template` 67 | - `OnDocFormSave` it will _only_ add the Resource to the table for automatic routing if there's a truth-y value in the TV with name `amp_tv`. If the TV is empty or false-y, it will remove the Resource from the table. This provides primitive "remove" functionality until such time a CMP is made to manage ContextResources. 68 | 69 | ## Example Implementation 70 | 71 | Check out the README on github.com: [https://github.com/modxcms/ampify](https://github.com/modxcms/ampify) -------------------------------------------------------------------------------- /core/components/ampify/elements/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/core/components/ampify/elements/.gitignore -------------------------------------------------------------------------------- /core/components/ampify/elements/plugins/ampify.plugin.php: -------------------------------------------------------------------------------- 1 | getOption('amp_url_param', $scriptProperties, ''); 13 | 14 | // AMPIFY Context 15 | $ampContext = $modx->getOption('amp_context', $scriptProperties, ''); 16 | 17 | // AMPIFY Mode 18 | $mode = 0; 19 | if (!empty($ampUrlParam)) { 20 | $mode = 'param'; 21 | } elseif (!empty($ampContext)) { 22 | $countCtx = $modx->getCount('modContext', array('key' => $ampContext)); 23 | if ($countCtx === 1) { 24 | $mode = 'context'; 25 | } 26 | } 27 | if ($mode === 0) { 28 | $modx->log(modX::LOG_LEVEL_WARN, 'AMPIFY requires at least a valid amp_context or an amp_url_param specified.'); 29 | return; 30 | } 31 | 32 | // AMPIFY Template 33 | $ampTemplate = $modx->getOption('amp_template', $scriptProperties, ''); 34 | $countTpl = $modx->getCount('modTemplate', $ampTemplate); 35 | if ($countTpl !== 1) { 36 | $modx->log(modX::LOG_LEVEL_ERROR, 'AMPIFY requires a valid, default Template ID'); 37 | return; 38 | } 39 | 40 | // AMPIFY TV 41 | $ampTv = $modx->getOption('amp_tv', $scriptProperties, ''); 42 | $countTv = $modx->getCount('modTemplateVar', array('name' => $ampTv)); 43 | 44 | $event = $modx->event->name; 45 | switch ($event) { 46 | 47 | case 'OnLoadWebDocument': 48 | 49 | // Escape conditions 50 | if ($mode === 'param' && !isset($_GET[$ampUrlParam])) { 51 | break; 52 | } 53 | if ($mode === 'context' && ($modx->context->get('key') !== $ampContext)) { 54 | break; 55 | } 56 | if (!($modx->resource instanceof modResource)) { 57 | break; 58 | } 59 | 60 | // Check Resource AMP TV 61 | if ($countTv === 1) { 62 | $tvValue = $modx->resource->getTVValue($ampTv); 63 | $countTvTpl = $modx->getCount('modTemplate', $tvValue); 64 | if ($countTvTpl === 1) $ampTemplate = $tvValue; 65 | } 66 | 67 | // Don't cache 'param' mode result 68 | if ($mode === 'param') $modx->resource->set('cacheable', 0); 69 | 70 | // Set runtime resource property 71 | $modx->resource->set('template', $ampTemplate); 72 | 73 | // Move on 74 | break; 75 | 76 | case 'OnDocFormSave': 77 | 78 | // Probably overly paranoid 79 | if ($modx->context->get('key') !== 'mgr') { 80 | break; 81 | } 82 | 83 | // If $mode isn't 'context', stop doing ContextResource actions 84 | if ($mode !== 'context') { 85 | break; 86 | } 87 | 88 | // Check Resource 89 | if (!($resource instanceof modResource)) { 90 | break; 91 | } 92 | 93 | // Check Resource AMP TV 94 | if ($countTv === 1) { 95 | $tvValue = $resource->getTVValue($ampTv); 96 | } 97 | 98 | // Set criteria for ContextResource object 99 | $criteria = array( 100 | 'context_key' => $ampContext, 101 | 'resource' => $resource->get('id'), 102 | ); 103 | 104 | // Check ContextResource 105 | $ctxRes = $modx->getObject('modContextResource', $criteria); 106 | 107 | // Remove if using the AMP TV and there' no tvValue 108 | if ($countTv === 1 && !$tvValue && $ctxRes instanceof modContextResource) { 109 | $ctxRes->remove(); 110 | break; 111 | } 112 | 113 | // Create if it doesn't exist 114 | if ($ctxRes === null) { 115 | 116 | $rc = $modx->newObject('modContextResource'); 117 | $rc->fromArray($criteria, '', true); //set pk 118 | // Save 119 | if (!$rc->save()) { 120 | $modx->log(modX::LOG_LEVEL_ERROR, 'AMPIFY could not save modContextResource: ' . print_r($criteria, true)); 121 | } 122 | // Trigger the Context Gateway 123 | $modx->cacheManager->refresh(); 124 | } 125 | 126 | // We're done 127 | break; 128 | 129 | // Don't do anything on other events 130 | default: 131 | break; 132 | 133 | } 134 | 135 | // The Plugin returns nothing 136 | return; -------------------------------------------------------------------------------- /core/components/ampify/elements/snippets/ampfilter.snippet.php: -------------------------------------------------------------------------------- 1 | 8 | * Copyright 2015 by YJ Tso 9 | * 10 | * This program is free software; you can redistribute it and/or modify it under 11 | * the terms of the GNU General Public License as published by the Free Software 12 | * Foundation; either version 2 of the License, or (at your option) any later 13 | * version. 14 | * 15 | * This program is distributed in the hope that it will be useful, but WITHOUT 16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 17 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 18 | * details. 19 | * 20 | * You should have received a copy of the GNU General Public License along with 21 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 22 | * Place, Suite 330, Boston, MA 02111-1307 USA 23 | **/ 24 | 25 | // Paths 26 | $ampifyPath = $modx->getOption('ampify.core_path', null, $modx->getOption('core_path') . 'components/ampify/'); 27 | $ampifyPath .= 'model/ampify/'; 28 | 29 | // Get Class 30 | if (file_exists($ampifyPath . 'ampify.class.php')) $ampify = $modx->getService('ampify', 'Ampify', $ampifyPath, $scriptProperties); 31 | if (!($ampify instanceof Ampify)) { 32 | $modx->log(modX::LOG_LEVEL_ERROR, '[ampfilter] could not load the required class!'); 33 | return; 34 | } 35 | 36 | $sanitizers = $modx->fromJSON($modx->getOption('options', $scriptProperties, '{"img": []}', true)); 37 | if (!is_array($sanitizers)) $sanitizers = array('img' => array()); 38 | 39 | $amp = $ampify->getAmpSterc($input, array(), $sanitizers, array()); 40 | return $amp->get_amp_content(); -------------------------------------------------------------------------------- /core/components/ampify/elements/templates/amp_template.template.tpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | [[*pagetitle]] 6 | 7 | 8 | 39 | 40 | 43 | 44 | 45 | 46 |

[[*pagetitle]]

47 | 48 |
[[*content]]
49 | 50 | 51 | -------------------------------------------------------------------------------- /core/components/ampify/index.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/modxcms/ampify/c79e439ab738e780fe69362daddc3a438a596b7e/core/components/ampify/index.php -------------------------------------------------------------------------------- /core/components/ampify/model/ampify/ampify.class.php: -------------------------------------------------------------------------------- 1 | 8 | * Copyright 2015 by YJ Tso 9 | * 10 | * This program is free software; you can redistribute it and/or modify it under 11 | * the terms of the GNU General Public License as published by the Free Software 12 | * Foundation; either version 2 of the License, or (at your option) any later 13 | * version. 14 | * 15 | * This program is distributed in the hope that it will be useful, but WITHOUT 16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 17 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 18 | * details. 19 | * 20 | * You should have received a copy of the GNU General Public License along with 21 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 22 | * Place, Suite 330, Boston, MA 02111-1307 USA 23 | **/ 24 | 25 | class Ampify 26 | { 27 | public $modx = null; 28 | public $namespace = 'ampify'; 29 | public $options = array(); 30 | public $amp = null; 31 | 32 | public function __construct(modX &$modx, array $options = array()) 33 | { 34 | $this->modx =& $modx; 35 | $this->namespace = $this->getOption('namespace', $options, 'ampify'); 36 | 37 | $corePath = $this->getOption('core_path', $options, $this->modx->getOption('core_path', null, MODX_CORE_PATH) . 'components/ampify/'); 38 | $assetsPath = $this->getOption('assets_path', $options, $this->modx->getOption('assets_path', null, MODX_ASSETS_PATH) . 'components/ampify/'); 39 | $assetsUrl = $this->getOption('assets_url', $options, $this->modx->getOption('assets_url', null, MODX_ASSETS_URL) . 'components/ampify/'); 40 | $dbPrefix = $this->getOption('table_prefix', $options, $this->modx->getOption('table_prefix', null, 'modx_')); 41 | 42 | /* load config defaults */ 43 | $this->options = array_merge(array( 44 | 'namespace' => $this->namespace, 45 | 'corePath' => $corePath, 46 | 'modelPath' => $corePath . 'model/', 47 | 'ampLibPath' => $corePath . 'model/amp-library/', 48 | 'ampLibPathSterc' => $corePath . 'model/modx-amp/', 49 | 'chunksPath' => $corePath . 'elements/chunks/', 50 | 'snippetsPath' => $corePath . 'elements/snippets/', 51 | 'templatesPath' => $corePath . 'templates/', 52 | 'assetsPath' => $assetsPath, 53 | 'assetsUrl' => $assetsUrl, 54 | 'jsUrl' => $assetsUrl . 'js/', 55 | 'cssUrl' => $assetsUrl . 'css/', 56 | 'connectorUrl' => $assetsUrl . 'connector.php', 57 | 58 | ), $options); 59 | 60 | } 61 | 62 | public function getAmp() { 63 | // Don't use this method right now 64 | return false; 65 | // Load AMP Library 66 | require_once($this->options['ampLibPath'] . 'vendor/autoload.php'); 67 | return new Lullabot\AMP\AMP(); 68 | } 69 | public function getAmpSterc($content, $embed_handler_classes, $sanitizers, $args = array()) { 70 | // Load AMP Library 71 | define('AMP__DIR__', rtrim($this->options['ampLibPathSterc'], '/')); 72 | require_once($this->options['ampLibPathSterc'] . 'class-amp-content.php'); 73 | 74 | // Load sanitizers 75 | $sanitizer_classes = array(); 76 | foreach ($sanitizers as $sanitizer => $args) { 77 | require_once($this->options['ampLibPathSterc'] . 'includes/sanitizers/class-amp-' . $sanitizer . '-sanitizer.php'); 78 | $sanitizer_classes['AMP_' . ucfirst($sanitizer) . '_Sanitizer'] = $args; 79 | } 80 | 81 | return new AMP_Content($content, $embed_handler_classes, $sanitizer_classes, $args); 82 | 83 | } 84 | 85 | /* UTILITY METHODS (@theboxer) */ 86 | /** 87 | * Get a local configuration option or a namespaced system setting by key. 88 | * 89 | * @param string $key The option key to search for. 90 | * @param array $options An array of options that override local options. 91 | * @param mixed $default The default value returned if the option is not found locally or as a 92 | * namespaced system setting; by default this value is null. 93 | * @return mixed The option value or the default value specified. 94 | */ 95 | public function getOption($key, $options = array(), $default = null) 96 | { 97 | $option = $default; 98 | if (!empty($key) && is_string($key)) { 99 | if ($options != null && array_key_exists($key, $options)) { 100 | $option = $options[$key]; 101 | } elseif (array_key_exists($key, $this->options)) { 102 | $option = $this->options[$key]; 103 | } elseif (array_key_exists("{$this->namespace}.{$key}", $this->modx->config)) { 104 | $option = $this->modx->getOption("{$this->namespace}.{$key}"); 105 | } 106 | } 107 | return $option; 108 | } 109 | 110 | public function explodeAndClean($array, $delimiter = ',') 111 | { 112 | $array = explode($delimiter, $array); // Explode fields to array 113 | $array = array_map('trim', $array); // Trim array's values 114 | $array = array_keys(array_flip($array)); // Remove duplicate fields 115 | $array = array_filter($array); // Remove empty values from array 116 | 117 | return $array; 118 | } 119 | public function getChunk($tpl, $phs) 120 | { 121 | if (strpos($tpl, '@INLINE ') !== false) { 122 | $content = str_replace('@INLINE', '', $tpl); 123 | /** @var \modChunk $chunk */ 124 | $chunk = $this->modx->newObject('modChunk', array('name' => 'inline-' . uniqid())); 125 | $chunk->setCacheable(false); 126 | 127 | return $chunk->process($phs, $content); 128 | } 129 | 130 | return $this->modx->getChunk($tpl, $phs); 131 | } 132 | } -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/class-amp-content.php: -------------------------------------------------------------------------------- 1 | content = $content; 18 | $this->args = $args; 19 | $this->embed_handler_classes = $embed_handler_classes; 20 | $this->sanitizer_classes = $sanitizer_classes; 21 | 22 | $this->transform(); 23 | } 24 | 25 | public function get_amp_content() { 26 | return $this->amp_content; 27 | } 28 | 29 | public function get_amp_scripts() { 30 | return $this->amp_scripts; 31 | } 32 | 33 | private function transform() { 34 | $content = $this->content; 35 | 36 | /* 37 | // First, embeds + the_content filter 38 | $embed_handlers = $this->register_embed_handlers(); 39 | $this->unregister_embed_handlers( $embed_handlers ); 40 | */ 41 | 42 | // Then, sanitize to strip and/or convert non-amp content 43 | $content = $this->sanitize( $content ); 44 | 45 | $this->amp_content = $content; 46 | } 47 | 48 | private function add_scripts( $scripts ) { 49 | $this->amp_scripts = array_merge( $this->amp_scripts, $scripts ); 50 | } 51 | /* 52 | private function register_embed_handlers() { 53 | $embed_handlers = array(); 54 | 55 | foreach ( $this->embed_handler_classes as $embed_handler_class => $args ) { 56 | $embed_handler = new $embed_handler_class( array_merge( $this->args, $args ) ); 57 | 58 | if ( ! is_subclass_of( $embed_handler, 'AMP_Base_Embed_Handler' ) ) { 59 | _doing_it_wrong( __METHOD__, sprintf( __( 'Embed Handler (%s) must extend `AMP_Embed_Handler`', 'amp' ), $embed_handler_class ), '0.1' ); 60 | continue; 61 | } 62 | 63 | $embed_handler->register_embed(); 64 | $embed_handlers[] = $embed_handler; 65 | } 66 | 67 | return $embed_handlers; 68 | } 69 | 70 | private function unregister_embed_handlers( $embed_handlers ) { 71 | foreach ( $embed_handlers as $embed_handler ) { 72 | $this->add_scripts( $embed_handler->get_scripts() ); 73 | $embed_handler->unregister_embed(); 74 | } 75 | } 76 | */ 77 | private function sanitize( $content ) { 78 | $dom = AMP_DOM_Utils::get_dom_from_content( $content ); 79 | 80 | foreach ( $this->sanitizer_classes as $sanitizer_class => $args ) { 81 | $sanitizer = new $sanitizer_class( $dom, array_merge( $this->args, $args ) ); 82 | 83 | if ( ! is_subclass_of( $sanitizer, 'AMP_Base_Sanitizer' ) ) { 84 | _doing_it_wrong( __METHOD__, sprintf( __( 'Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp' ), esc_html( $sanitizer_class ) ), '0.1' ); 85 | continue; 86 | } 87 | 88 | $sanitizer->sanitize(); 89 | $this->add_scripts( $sanitizer->get_scripts() ); 90 | } 91 | 92 | return AMP_DOM_Utils::get_content_from_dom( $dom ); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/lib/class-fastimage.php: -------------------------------------------------------------------------------- 1 | load($uri); 24 | } 25 | 26 | 27 | public function load($uri) 28 | { 29 | if ($this->handle) $this->close(); 30 | 31 | $this->handle = fopen($uri, 'r'); 32 | } 33 | 34 | 35 | public function close() 36 | { 37 | if ($this->handle) 38 | { 39 | fclose($this->handle); 40 | $this->handle = null; 41 | $this->type = null; 42 | $this->str = null; 43 | } 44 | } 45 | 46 | 47 | public function getSize() 48 | { 49 | if (!$this->handle) 50 | { 51 | return false; 52 | } 53 | 54 | $this->strpos = 0; 55 | if ($this->getType()) 56 | { 57 | return array_values($this->parseSize()); 58 | } 59 | 60 | return false; 61 | } 62 | 63 | 64 | public function getType() 65 | { 66 | if (!$this->handle) 67 | { 68 | return false; 69 | } 70 | 71 | $this->strpos = 0; 72 | 73 | if (!$this->type) 74 | { 75 | switch ($this->getChars(2)) 76 | { 77 | case "BM": 78 | return $this->type = 'bmp'; 79 | case "GI": 80 | return $this->type = 'gif'; 81 | case chr(0xFF).chr(0xd8): 82 | return $this->type = 'jpeg'; 83 | case chr(0x89).'P': 84 | return $this->type = 'png'; 85 | default: 86 | return false; 87 | } 88 | } 89 | 90 | return $this->type; 91 | } 92 | 93 | 94 | private function parseSize() 95 | { 96 | $this->strpos = 0; 97 | 98 | switch ($this->type) 99 | { 100 | case 'png': 101 | return $this->parseSizeForPNG(); 102 | case 'gif': 103 | return $this->parseSizeForGIF(); 104 | case 'bmp': 105 | return $this->parseSizeForBMP(); 106 | case 'jpeg': 107 | return $this->parseSizeForJPEG(); 108 | } 109 | 110 | return null; 111 | } 112 | 113 | 114 | private function parseSizeForPNG() 115 | { 116 | $chars = $this->getChars(25); 117 | 118 | return unpack("N*", substr($chars, 16, 8)); 119 | } 120 | 121 | 122 | private function parseSizeForGIF() 123 | { 124 | $chars = $this->getChars(11); 125 | 126 | return unpack("S*", substr($chars, 6, 4)); 127 | } 128 | 129 | 130 | private function parseSizeForBMP() 131 | { 132 | $chars = $this->getChars(29); 133 | $chars = substr($chars, 14, 14); 134 | $type = unpack('C', $chars); 135 | 136 | return (reset($type) == 40) ? unpack('L*', substr($chars, 4)) : unpack('L*', substr($chars, 4, 8)); 137 | } 138 | 139 | 140 | private function parseSizeForJPEG() 141 | { 142 | $state = null; 143 | $i = 0; 144 | 145 | while (true) 146 | { 147 | switch ($state) 148 | { 149 | default: 150 | $this->getChars(2); 151 | $state = 'started'; 152 | break; 153 | 154 | case 'started': 155 | $b = $this->getByte(); 156 | if ($b === false) return false; 157 | 158 | $state = $b == 0xFF ? 'sof' : 'started'; 159 | break; 160 | 161 | case 'sof': 162 | $b = $this->getByte(); 163 | if (in_array($b, range(0xe0, 0xef))) 164 | { 165 | $state = 'skipframe'; 166 | } 167 | elseif (in_array($b, array_merge(range(0xC0,0xC3), range(0xC5,0xC7), range(0xC9,0xCB), range(0xCD,0xCF)))) 168 | { 169 | $state = 'readsize'; 170 | } 171 | elseif ($b == 0xFF) 172 | { 173 | $state = 'sof'; 174 | } 175 | else 176 | { 177 | $state = 'skipframe'; 178 | } 179 | break; 180 | 181 | case 'skipframe': 182 | $skip = $this->readInt($this->getChars(2)) - 2; 183 | $state = 'doskip'; 184 | break; 185 | 186 | case 'doskip': 187 | $this->getChars($skip); 188 | $state = 'started'; 189 | break; 190 | 191 | case 'readsize': 192 | $c = $this->getChars(7); 193 | 194 | return array($this->readInt(substr($c, 5, 2)), $this->readInt(substr($c, 3, 2))); 195 | } 196 | } 197 | } 198 | 199 | 200 | private function getChars($n) 201 | { 202 | $response = null; 203 | 204 | // do we need more data? 205 | if ($this->strpos + $n -1 >= strlen($this->str)) 206 | { 207 | $end = ($this->strpos + $n); 208 | 209 | while (strlen($this->str) < $end && $response !== false) 210 | { 211 | // read more from the file handle 212 | $need = $end - ftell($this->handle); 213 | 214 | if ($response = fread($this->handle, $need)) 215 | { 216 | $this->str .= $response; 217 | } 218 | else 219 | { 220 | return false; 221 | } 222 | } 223 | } 224 | 225 | $result = substr($this->str, $this->strpos, $n); 226 | $this->strpos += $n; 227 | 228 | return $result; 229 | } 230 | 231 | 232 | private function getByte() 233 | { 234 | $c = $this->getChars(1); 235 | $b = unpack("C", $c); 236 | 237 | return reset($b); 238 | } 239 | 240 | 241 | private function readInt($str) 242 | { 243 | $size = unpack("C*", $str); 244 | 245 | return ($size[1] << 8) + $size[2]; 246 | } 247 | 248 | 249 | public function __destruct() 250 | { 251 | $this->close(); 252 | } 253 | } 254 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/sanitizers/class-amp-audio-sanitizer.php: -------------------------------------------------------------------------------- 1 | did_convert_elements ) { 13 | return array(); 14 | } 15 | 16 | return array( self::$script_slug => self::$script_src ); 17 | } 18 | 19 | public function sanitize() { 20 | $nodes = $this->dom->getElementsByTagName( self::$tag ); 21 | $num_nodes = $nodes->length; 22 | if ( 0 === $num_nodes ) { 23 | return; 24 | } 25 | 26 | for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { 27 | $node = $nodes->item( $i ); 28 | $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); 29 | 30 | $new_attributes = $this->filter_attributes( $old_attributes ); 31 | 32 | $new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-audio', $new_attributes ); 33 | 34 | // TODO: limit child nodes too (only allowed: `source`; move rest to div+fallback) 35 | // TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well. 36 | foreach ( $node->childNodes as $child_node ) { 37 | $new_child_node = $child_node->cloneNode( true ); 38 | $new_node->appendChild( $new_child_node ); 39 | } 40 | 41 | $node->parentNode->replaceChild( $new_node, $node ); 42 | 43 | $this->did_convert_elements = true; 44 | } 45 | } 46 | 47 | private function filter_attributes( $attributes ) { 48 | $out = array(); 49 | 50 | foreach ( $attributes as $name => $value ) { 51 | switch ( $name ) { 52 | case 'src': 53 | case 'width': 54 | case 'height': 55 | case 'class': 56 | $out[ $name ] = $value; 57 | break; 58 | case 'loop': 59 | case 'muted': 60 | if ( 'false' !== $value ) { 61 | $out[ $name ] = 'true'; 62 | } 63 | break; 64 | case 'autoplay': 65 | $out[ $name ] = 'desktop tablet mobile'; 66 | break; 67 | default; 68 | break; 69 | } 70 | } 71 | 72 | return $out; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/sanitizers/class-amp-base-sanitizer.php: -------------------------------------------------------------------------------- 1 | dom = $dom; 12 | $this->args = array_merge( $this->DEFAULT_ARGS, $args ); 13 | } 14 | 15 | abstract public function sanitize(); 16 | 17 | public function get_scripts() { 18 | return array(); 19 | } 20 | 21 | protected function get_body_node() { 22 | return $this->dom->getElementsByTagName( 'body' )->item( 0 ); 23 | } 24 | 25 | /** 26 | * This is our workaround to enforce max sizing with layout=responsive. 27 | * 28 | * We want elements to not grow beyond their width and shrink to fill the screen on viewports smaller than their width. 29 | * 30 | * See https://github.com/ampproject/amphtml/issues/1280#issuecomment-171533526 31 | * See https://github.com/Automattic/amp-wp/issues/101 32 | */ 33 | public function enforce_sizes_attribute( $attributes ) { 34 | if ( ! isset( $attributes['width'], $attributes['height'] ) ) { 35 | return $attributes; 36 | } 37 | 38 | $max_width = $attributes['width']; 39 | if ( isset( $this->args['content_max_width'] ) && $max_width >= $this->args['content_max_width'] ) { 40 | $max_width = $this->args['content_max_width']; 41 | } 42 | 43 | $attributes['sizes'] = sprintf( '(min-width: %1$dpx) %1$dpx, 100vw', abs(intval($max_width)) ); 44 | 45 | $this->add_or_append_attribute( $attributes, 'class', 'amp-wp-enforced-sizes' ); 46 | 47 | return $attributes; 48 | } 49 | 50 | public function add_or_append_attribute( &$attributes, $key, $value, $separator = ' ' ) { 51 | if ( isset( $attributes[ $key ] ) ) { 52 | $attributes[ $key ] .= $separator . $value; 53 | } else { 54 | $attributes[ $key ] = $value; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/sanitizers/class-amp-blacklist-sanitizer.php: -------------------------------------------------------------------------------- 1 | get_blacklisted_tags(); 16 | $blacklisted_attributes = $this->get_blacklisted_attributes(); 17 | $blacklisted_protocols = $this->get_blacklisted_protocols(); 18 | 19 | $body = $this->get_body_node(); 20 | $this->strip_tags( $body, $blacklisted_tags ); 21 | $this->strip_attributes_recursive( $body, $blacklisted_attributes, $blacklisted_protocols ); 22 | } 23 | 24 | private function strip_attributes_recursive( $node, $bad_attributes, $bad_protocols ) { 25 | if ( $node->nodeType !== XML_ELEMENT_NODE ) { 26 | return; 27 | } 28 | 29 | if ( $node->hasAttributes() ) { 30 | $node_name = $node->nodeName; 31 | $length = $node->attributes->length; 32 | for ( $i = $length - 1; $i >= 0; $i-- ) { 33 | $attribute = $node->attributes->item( $i ); 34 | $attribute_name = strtolower( $attribute->name ); 35 | if ( in_array( $attribute_name, $bad_attributes ) ) { 36 | $node->removeAttribute( $attribute_name ); 37 | continue; 38 | } 39 | 40 | // on* attributes (like onclick) are a special case 41 | if ( 0 === stripos( $attribute_name, 'on' ) && $attribute_name != 'on' ) { 42 | $node->removeAttribute( $attribute_name ); 43 | continue; 44 | } elseif ( 'href' === $attribute_name ) { 45 | $protocol = strtok( $attribute->value, ':' ); 46 | if ( in_array( $protocol, $bad_protocols ) ) { 47 | $node->removeAttribute( $attribute_name ); 48 | continue; 49 | } 50 | } elseif ( 'a' === $node_name ) { 51 | $this->sanitize_a_attribute( $node, $attribute ); 52 | } 53 | } 54 | } 55 | 56 | foreach ( $node->childNodes as $child_node ) { 57 | $this->strip_attributes_recursive( $child_node, $bad_attributes, $bad_protocols ); 58 | } 59 | } 60 | 61 | private function strip_tags( $node, $tag_names ) { 62 | foreach ( $tag_names as $tag_name ) { 63 | $elements = $node->getElementsByTagName( $tag_name ); 64 | $length = $elements->length; 65 | if ( 0 === $length ) { 66 | continue; 67 | } 68 | 69 | for ( $i = $length - 1; $i >= 0; $i-- ) { 70 | $element = $elements->item( $i ); 71 | $parent_node = $element->parentNode; 72 | $parent_node->removeChild( $element ); 73 | 74 | if ( 'body' !== $parent_node->nodeName && AMP_DOM_Utils::is_node_empty( $parent_node ) ) { 75 | $parent_node->parentNode->removeChild( $parent_node ); 76 | } 77 | } 78 | } 79 | } 80 | 81 | private function sanitize_a_attribute( $node, $attribute ) { 82 | $attribute_name = strtolower( $attribute->name ); 83 | 84 | if ( 'rel' === $attribute_name ) { 85 | $old_value = $attribute->value; 86 | $new_value = trim( preg_replace( self::PATTERN_REL_WP_ATTACHMENT, '', $old_value ) ); 87 | if ( empty( $new_value ) ) { 88 | $node->removeAttribute( $attribute_name ); 89 | } elseif ( $old_value !== $new_value ) { 90 | $node->setAttribute( $attribute_name, $new_value ); 91 | } 92 | } elseif ( 'rev' === $attribute_name ) { 93 | // rev removed from HTML5 spec, which was used by Jetpack Markdown. 94 | $node->removeAttribute( $attribute_name ); 95 | } elseif ( 'target' === $attribute_name && '_new' === $attribute->value ) { 96 | // _new is not allowed; swap with _blank 97 | $node->setAttribute( $attribute_name, '_blank' ); 98 | } 99 | } 100 | 101 | private function get_blacklisted_protocols() { 102 | return array( 103 | 'javascript', 104 | ); 105 | } 106 | 107 | private function get_blacklisted_tags() { 108 | return array( 109 | 'script', 110 | 'noscript', 111 | 'style', 112 | 'frame', 113 | 'frameset', 114 | 'object', 115 | 'param', 116 | 'applet', 117 | 'form', 118 | 'label', 119 | 'input', 120 | 'textarea', 121 | 'select', 122 | 'option', 123 | 'link', 124 | 125 | // These are converted into amp-* versions 126 | //'img', 127 | //'video', 128 | //'audio', 129 | //'iframe', 130 | ); 131 | } 132 | 133 | private function get_blacklisted_attributes() { 134 | return array( 135 | 'style', 136 | ); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/sanitizers/class-amp-iframe-sanitizer.php: -------------------------------------------------------------------------------- 1 | tags to 7 | */ 8 | class AMP_Iframe_Sanitizer extends AMP_Base_Sanitizer { 9 | const FALLBACK_HEIGHT = 400; 10 | const SANDBOX_DEFAULTS = 'allow-scripts allow-same-origin'; 11 | 12 | public static $tag = 'iframe'; 13 | 14 | private static $script_slug = 'amp-iframe'; 15 | private static $script_src = 'https://cdn.ampproject.org/v0/amp-iframe-0.1.js'; 16 | 17 | protected $DEFAULT_ARGS = array( 18 | 'add_placeholder' => false, 19 | ); 20 | 21 | public function get_scripts() { 22 | if ( ! $this->did_convert_elements ) { 23 | return array(); 24 | } 25 | 26 | return array( self::$script_slug => self::$script_src ); 27 | } 28 | 29 | public function sanitize() { 30 | $nodes = $this->dom->getElementsByTagName( self::$tag ); 31 | $num_nodes = $nodes->length; 32 | if ( 0 === $num_nodes ) { 33 | return; 34 | } 35 | 36 | for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { 37 | $node = $nodes->item( $i ); 38 | $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); 39 | 40 | if ( ! array_key_exists( 'src', $old_attributes ) ) { 41 | $node->parentNode->removeChild( $node ); 42 | continue; 43 | } 44 | 45 | $this->did_convert_elements = true; 46 | 47 | $new_attributes = $this->filter_attributes( $old_attributes ); 48 | 49 | if ( ! isset( $new_attributes['height'] ) ) { 50 | unset( $new_attributes['width'] ); 51 | $new_attributes['height'] = self::FALLBACK_HEIGHT; 52 | } 53 | 54 | if ( ! isset( $new_attributes['width'] ) ) { 55 | $new_attributes['layout'] = 'fixed-height'; 56 | } 57 | $new_attributes = $this->enforce_sizes_attribute( $new_attributes ); 58 | 59 | $new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-iframe', $new_attributes ); 60 | 61 | if ( true === $this->args['add_placeholder'] ) { 62 | $placeholder_node = $this->build_placeholder( $new_attributes ); 63 | $new_node->appendChild( $placeholder_node ); 64 | } 65 | 66 | $parent_node = $node->parentNode; 67 | if ( 'p' === strtolower( $parent_node->tagName ) ) { 68 | // AMP does not like iframes in p tags 69 | $parent_node->removeChild( $node ); 70 | $parent_node->parentNode->insertBefore( $new_node, $parent_node->nextSibling ); 71 | 72 | if ( AMP_DOM_Utils::is_node_empty( $parent_node ) ) { 73 | $parent_node->parentNode->removeChild( $parent_node ); 74 | } 75 | } else { 76 | $parent_node->replaceChild( $new_node, $node ); 77 | } 78 | } 79 | } 80 | 81 | private function filter_attributes( $attributes ) { 82 | $out = array(); 83 | 84 | foreach ( $attributes as $name => $value ) { 85 | switch ( $name ) { 86 | case 'src': 87 | case 'sandbox': 88 | case 'height': 89 | case 'class': 90 | case 'sizes': 91 | $out[ $name ] = $value; 92 | break; 93 | 94 | case 'width': 95 | if ( $value === '100%' ) { 96 | continue; 97 | } 98 | $out[ $name ] = $value; 99 | break; 100 | 101 | case 'frameborder': 102 | if ( '0' !== $value && '1' !== $value ) { 103 | $value = '0'; 104 | } 105 | $out[ $name ] = $value; 106 | break; 107 | 108 | case 'allowfullscreen': 109 | case 'allowtransparency': 110 | if ( 'false' !== $value ) { 111 | $out[ $name ] = ''; 112 | } 113 | break; 114 | 115 | default; 116 | break; 117 | } 118 | } 119 | 120 | if ( ! isset( $out[ 'sandbox' ] ) ) { 121 | $out[ 'sandbox' ] = self::SANDBOX_DEFAULTS; 122 | } 123 | 124 | return $out; 125 | } 126 | 127 | private function build_placeholder( $parent_attributes ) { 128 | $placeholder_node = AMP_DOM_Utils::create_node( $this->dom, 'div', array( 129 | 'placeholder' => '', 130 | 'class' => 'amp-wp-iframe-placeholder', 131 | ) ); 132 | 133 | return $placeholder_node; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/sanitizers/class-amp-img-sanitizer.php: -------------------------------------------------------------------------------- 1 | tags to or 7 | */ 8 | class AMP_Img_Sanitizer extends AMP_Base_Sanitizer { 9 | const FALLBACK_WIDTH = 600; 10 | const FALLBACK_HEIGHT = 400; 11 | 12 | public static $tag = 'img'; 13 | 14 | private static $anim_extension = '.gif'; 15 | 16 | private static $script_slug = 'amp-anim'; 17 | private static $script_src = 'https://cdn.ampproject.org/v0/amp-anim-0.1.js'; 18 | 19 | public function sanitize() { 20 | 21 | $nodes = $this->dom->getElementsByTagName( self::$tag ); 22 | $num_nodes = $nodes->length; 23 | if ( 0 === $num_nodes ) { 24 | return; 25 | } 26 | 27 | for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { 28 | $node = $nodes->item( $i ); 29 | $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); 30 | 31 | if ( ! array_key_exists( 'src', $old_attributes ) ) { 32 | $node->parentNode->removeChild( $node ); 33 | continue; 34 | } 35 | 36 | $new_attributes = $this->filter_attributes( $old_attributes ); 37 | 38 | // Try to extract dimensions for the image, if not set. 39 | if ( ! isset( $new_attributes['width'] ) || ! isset( $new_attributes['height'] ) ) { 40 | $dimensions = getimagesize($new_attributes['src']); 41 | if ( is_array( $dimensions ) ) { 42 | $new_attributes['width'] = $dimensions[0]; 43 | $new_attributes['height'] = $dimensions[1]; 44 | } 45 | } 46 | 47 | // Final fallback when we have no dimensions. 48 | if ( ! isset( $new_attributes['width'] ) || ! isset( $new_attributes['height'] ) ) { 49 | $new_attributes['width'] = isset( $this->args['content_max_width'] ) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH; 50 | $new_attributes['height'] = self::FALLBACK_HEIGHT; 51 | 52 | $this->add_or_append_attribute( $new_attributes, 'class', 'amp-wp-unknown-size' ); 53 | } 54 | 55 | $new_attributes = $this->enforce_sizes_attribute( $new_attributes ); 56 | 57 | if ( $this->args['animGifs'] && $this->is_gif_url( $new_attributes['src'] ) ) { 58 | $this->did_convert_elements = true; 59 | $new_tag = 'amp-anim'; 60 | } else { 61 | $new_tag = 'amp-img'; 62 | } 63 | 64 | $new_node = AMP_DOM_Utils::create_node( $this->dom, $new_tag, $new_attributes ); 65 | $node->parentNode->replaceChild( $new_node, $node ); 66 | } 67 | } 68 | 69 | public function get_scripts() { 70 | if ( ! $this->did_convert_elements ) { 71 | return array(); 72 | } 73 | 74 | return array( self::$script_slug => self::$script_src ); 75 | } 76 | 77 | private function filter_attributes( $attributes ) { 78 | $out = array(); 79 | 80 | foreach ( $attributes as $name => $value ) { 81 | switch ( $name ) { 82 | case 'src': 83 | case 'alt': 84 | case 'width': 85 | case 'height': 86 | case 'class': 87 | case 'srcset': 88 | case 'sizes': 89 | case 'on': 90 | $out[ $name ] = $value; 91 | break; 92 | default; 93 | break; 94 | } 95 | } 96 | 97 | return $out; 98 | } 99 | 100 | private function is_gif_url( $url ) { 101 | $ext = self::$anim_extension; 102 | $path = parse_url( $url, PHP_URL_PATH ); 103 | return $ext === substr( $path, -strlen( $ext ) ); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/sanitizers/class-amp-video-sanitizer.php: -------------------------------------------------------------------------------- 1 | tags to 7 | */ 8 | class AMP_Video_Sanitizer extends AMP_Base_Sanitizer { 9 | const FALLBACK_HEIGHT = 400; 10 | 11 | public static $tag = 'video'; 12 | 13 | public function sanitize() { 14 | $nodes = $this->dom->getElementsByTagName( self::$tag ); 15 | $num_nodes = $nodes->length; 16 | if ( 0 === $num_nodes ) { 17 | return; 18 | } 19 | 20 | for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { 21 | $node = $nodes->item( $i ); 22 | $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); 23 | 24 | $new_attributes = $this->filter_attributes( $old_attributes ); 25 | if ( ! isset( $new_attributes['width'], $new_attributes['height'] ) ) { 26 | $new_attributes['height'] = self::FALLBACK_HEIGHT; 27 | $new_attributes['layout'] = 'fixed-height'; 28 | } 29 | $new_attributes = $this->enforce_sizes_attribute( $new_attributes ); 30 | 31 | $new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-video', $new_attributes ); 32 | 33 | // TODO: limit child nodes too (only allowed: `source`; move rest to div+fallback) 34 | // TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well. 35 | foreach ( $node->childNodes as $child_node ) { 36 | $new_child_node = $child_node->cloneNode( true ); 37 | $new_node->appendChild( $new_child_node ); 38 | } 39 | 40 | $node->parentNode->replaceChild( $new_node, $node ); 41 | } 42 | } 43 | 44 | private function filter_attributes( $attributes ) { 45 | $out = array(); 46 | 47 | foreach ( $attributes as $name => $value ) { 48 | switch ( $name ) { 49 | case 'src': 50 | case 'poster': 51 | case 'width': 52 | case 'height': 53 | case 'class': 54 | case 'sizes': 55 | $out[ $name ] = $value; 56 | break; 57 | case 'controls': 58 | case 'loop': 59 | case 'muted': 60 | if ( 'false' !== $value ) { 61 | $out[ $name ] = 'true'; 62 | } 63 | break; 64 | case 'autoplay': 65 | $out[ $name ] = 'desktop tablet mobile'; 66 | break; 67 | default; 68 | break; 69 | } 70 | } 71 | 72 | return $out; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/utils/class-amp-dom-utils.php: -------------------------------------------------------------------------------- 1 | loadHTML( '' . $content . '' ); 13 | 14 | libxml_clear_errors(); 15 | libxml_use_internal_errors( $libxml_previous_state ); 16 | 17 | if ( ! $result ) { 18 | return false; 19 | } 20 | 21 | return $dom; 22 | } 23 | 24 | public static function get_content_from_dom( $dom ) { 25 | // Only want children of the body tag, since we have a subset of HTML. 26 | $out = ''; 27 | $body = $dom->getElementsByTagName( 'body' )->item( 0 ); 28 | foreach ( $body->childNodes as $node ) { 29 | $out .= $dom->saveXML( $node, LIBXML_NOEMPTYTAG ); 30 | } 31 | return $out; 32 | } 33 | 34 | public static function create_node( $dom, $tag, $attributes ) { 35 | $node = $dom->createElement( $tag ); 36 | self::add_attributes_to_node( $node, $attributes ); 37 | return $node; 38 | } 39 | 40 | public static function get_node_attributes_as_assoc_array( $node ) { 41 | $attributes = array(); 42 | foreach ( $node->attributes as $attribute ) { 43 | $attributes[ $attribute->nodeName ] = $attribute->nodeValue; 44 | } 45 | return $attributes; 46 | } 47 | 48 | public static function add_attributes_to_node( $node, $attributes ) { 49 | foreach ( $attributes as $name => $value ) { 50 | $node->setAttribute( $name, $value ); 51 | } 52 | } 53 | 54 | public static function is_node_empty( $node ) { 55 | return 0 === $node->childNodes->length && empty( $node->textContent ); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /core/components/ampify/model/modx-amp/includes/utils/class-amp-html-utils.php: -------------------------------------------------------------------------------- 1 | %3$s', sanitize_key( $tag_name ), $attr_string, $content ); 7 | } 8 | 9 | public static function build_attributes_string( $attributes ) { 10 | $string = array(); 11 | foreach ( $attributes as $name => $value ) { 12 | if ( '' === $value ) { 13 | $string[] = sprintf( '%s', sanitize_key( $name ) ); 14 | } else { 15 | $string[] = sprintf( '%s="%s"', sanitize_key( $name ), esc_attr( $value ) ); 16 | } 17 | } 18 | return implode( ' ', $string ); 19 | } 20 | } 21 | --------------------------------------------------------------------------------