|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SEO Update Service Notifier Controller File. |
| 4 | + * |
| 5 | + * @author Oliver Green <[email protected]> |
| 6 | + * @license See attached license file |
| 7 | + */ |
| 8 | +namespace Concrete\Package\SeoUpdateServiceNotifier; |
| 9 | + |
| 10 | +use Core; |
| 11 | +use Concrete\Core\Foundation\Service\ProviderList; |
| 12 | +use Concrete\Core\Package\Package; |
| 13 | +use Illuminate\Filesystem\Filesystem; |
| 14 | + |
| 15 | +defined('C5_EXECUTE') or die('Access Denied.'); |
| 16 | + |
| 17 | +/** |
| 18 | + * Package Controller Class. |
| 19 | + * |
| 20 | + * Start building standards complient concrete5 pacakges from me. |
| 21 | + * |
| 22 | + * @author Oliver Green <[email protected]> |
| 23 | + * @license See attached license file |
| 24 | + */ |
| 25 | +class Controller extends Package |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Minimum version of concrete5 required to use this package. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $appVersionRequired = '5.7.0'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Does the package provide a full content swap? |
| 36 | + * This feature is often used in theme packages to install 'sample' content on the site. |
| 37 | + * |
| 38 | + * @see https://goo.gl/C4m6BG |
| 39 | + * @var bool |
| 40 | + */ |
| 41 | + protected $pkgAllowsFullContentSwap = false; |
| 42 | + |
| 43 | + /** |
| 44 | + * Does the package provide thumbnails of the files |
| 45 | + * imported via the full content swap above? |
| 46 | + * |
| 47 | + * @see https://goo.gl/C4m6BG |
| 48 | + * @var bool |
| 49 | + */ |
| 50 | + protected $pkgContentProvidesFileThumbnails = false; |
| 51 | + |
| 52 | + /** |
| 53 | + * Should we remove 'Src' from classes that are contained |
| 54 | + * ithin the packages 'src/Concrete' directory automatically? |
| 55 | + * |
| 56 | + * '\Concrete\Package\MyPackage\Src\MyNamespace' becomes '\Concrete\Package\MyPackage\MyNamespace' |
| 57 | + * |
| 58 | + * @see https://goo.gl/4wyRtH |
| 59 | + * @var bool |
| 60 | + */ |
| 61 | + protected $pkgAutoloaderMapCoreExtensions = false; |
| 62 | + |
| 63 | + /** |
| 64 | + * Package class autoloader registrations |
| 65 | + * The package install helper class, included with this boilerplate, |
| 66 | + * is activated by default. |
| 67 | + * |
| 68 | + * @see https://goo.gl/4wyRtH |
| 69 | + * @var array |
| 70 | + */ |
| 71 | + protected $pkgAutoloaderRegistries = [ |
| 72 | + //'src/MyVendor/Statistics' => '\MyVendor\ConcreteStatistics' |
| 73 | + ]; |
| 74 | + |
| 75 | + /** |
| 76 | + * The packages handle. |
| 77 | + * Note that this must be unique in the |
| 78 | + * entire concrete5 package ecosystem. |
| 79 | + * |
| 80 | + * @var string |
| 81 | + */ |
| 82 | + protected $pkgHandle = 'seo_update_service_notifier'; |
| 83 | + |
| 84 | + /** |
| 85 | + * The packages version. |
| 86 | + * |
| 87 | + * @var string |
| 88 | + */ |
| 89 | + protected $pkgVersion = '0.9.0'; |
| 90 | + |
| 91 | + /** |
| 92 | + * The packages name. |
| 93 | + * |
| 94 | + * @var string |
| 95 | + */ |
| 96 | + protected $pkgName = 'SEO Update Service Notifier'; |
| 97 | + |
| 98 | + /** |
| 99 | + * The packages description. |
| 100 | + * |
| 101 | + * @var string |
| 102 | + */ |
| 103 | + protected $pkgDescription = 'Notify popular update services that you have updated your site.'; |
| 104 | + |
| 105 | + /** |
| 106 | + * Package service providers to register. |
| 107 | + * |
| 108 | + * @var array |
| 109 | + */ |
| 110 | + protected $providers = [ |
| 111 | + // Register your concrete5 service providers here |
| 112 | + 'Concrete\Package\SeoUpdateServiceNotifier\Src\Providers\UpdateServiceNotifierServiceProvider', |
| 113 | + ]; |
| 114 | + |
| 115 | + /** |
| 116 | + * How often should we allow pinging? |
| 117 | + * |
| 118 | + * @var integer |
| 119 | + */ |
| 120 | + protected $rate_limit_ttl = 43200; // Every 12 hours |
| 121 | + |
| 122 | + /** |
| 123 | + * Register the packages defined service providers. |
| 124 | + * |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + protected function registerServiceProviders() |
| 128 | + { |
| 129 | + $list = new ProviderList(Core::getFacadeRoot()); |
| 130 | + |
| 131 | + foreach ($this->providers as $provider) { |
| 132 | + $list->registerProvider($provider); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * The packages on start hook that is fired as the CMS is booting up. |
| 138 | + * |
| 139 | + * @return void |
| 140 | + */ |
| 141 | + public function on_start() |
| 142 | + { |
| 143 | + // Register defined service providers |
| 144 | + $this->registerServiceProviders(); |
| 145 | + |
| 146 | + // Plugin the event listeners. |
| 147 | + $this->addEventListeners(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Get the list of update service URLs to ping. |
| 152 | + * |
| 153 | + * @return array |
| 154 | + */ |
| 155 | + protected function getUpdateServiceUrls() |
| 156 | + { |
| 157 | + return [ |
| 158 | + 'https://requestb.in/1i2ftut1' |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Checks to see whether we should ping the update services. |
| 164 | + * Currently this only checks that we are not above our rate limit. |
| 165 | + * |
| 166 | + * @return boolean |
| 167 | + */ |
| 168 | + protected function canPing() |
| 169 | + { |
| 170 | + $cache = Core::make('cache/expensive'); |
| 171 | + $item = $cache->getItem('seo.update.notifier.expiry'); |
| 172 | + |
| 173 | + if ($item->isMiss()) { |
| 174 | + $item->set(time() + $this->rate_limit_ttl, $this->rate_limit_ttl); |
| 175 | + |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Notifys update services that a page has been added / updated |
| 184 | + * and should be (re)indexed. |
| 185 | + * |
| 186 | + * @param \Page $page [description] |
| 187 | + * @return boolean |
| 188 | + */ |
| 189 | + protected function pingPage(\Page $page) |
| 190 | + { |
| 191 | + if ($this->canPing()) { |
| 192 | + $notifier = \Core::make('seo.update.notifier'); |
| 193 | + |
| 194 | + return $notifier->ping( |
| 195 | + $this->getUpdateServiceUrls(), |
| 196 | + \Config::get('concrete.site'), |
| 197 | + \View::url('/'), |
| 198 | + \View::url($page->getCollectionPath()) |
| 199 | + ); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Wires the page event listeners to the update service |
| 205 | + * notification service. |
| 206 | + * |
| 207 | + * @return void |
| 208 | + */ |
| 209 | + public function addEventListeners() |
| 210 | + { |
| 211 | + \Events::addListener('on_page_version_approve', function($event) { |
| 212 | + $page_id = $event->getCollectionVersionObject()->getCollectionID(); |
| 213 | + $page = \Page::getById($page_id); |
| 214 | + $this->pingPage($page); |
| 215 | + }); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * The packages install routine. |
| 220 | + * |
| 221 | + * @return \Concrete\Core\Package\Package |
| 222 | + */ |
| 223 | + public function install() |
| 224 | + { |
| 225 | + // Add your custom logic here that needs to be executed BEFORE package install. |
| 226 | + |
| 227 | + $pkg = parent::install(); |
| 228 | + |
| 229 | + // Add your custom logic here that needs to be executed AFTER package install. |
| 230 | + |
| 231 | + return $pkg; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * The packages upgrade routine. |
| 236 | + * |
| 237 | + * @return void |
| 238 | + */ |
| 239 | + public function upgrade() |
| 240 | + { |
| 241 | + // Add your custom logic here that needs to be executed BEFORE package install. |
| 242 | + |
| 243 | + parent::upgrade(); |
| 244 | + |
| 245 | + // Add your custom logic here that needs to be executed AFTER package upgrade. |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * The packages uninstall routine. |
| 250 | + * |
| 251 | + * @return void |
| 252 | + */ |
| 253 | + public function uninstall() |
| 254 | + { |
| 255 | + // Add your custom logic here that needs to be executed BEFORE package uninstall. |
| 256 | + |
| 257 | + parent::uninstall(); |
| 258 | + |
| 259 | + // Add your custom logic here that needs to be executed AFTER package uninstall. |
| 260 | + } |
| 261 | +} |
0 commit comments