rss.php
185 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments - Libraries |
| 4 | * @subpackage dashboard |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Widget used to display the latest RSS feeds within the |
| 16 | * admin dashboard of WordPress. |
| 17 | * |
| 18 | * @since 1.1.9 |
| 19 | */ |
| 20 | class JDashboardWidgetVikAppointmentsRss extends JDashboardWidget |
| 21 | { |
| 22 | /** |
| 23 | * The RSS reader class handler. |
| 24 | * |
| 25 | * @var JRssReader |
| 26 | */ |
| 27 | protected $rss; |
| 28 | |
| 29 | /** |
| 30 | * Class constructor. |
| 31 | */ |
| 32 | public function __construct() |
| 33 | { |
| 34 | // set up RSS reader |
| 35 | $this->rss = VikAppointmentsBuilder::setupRssReader(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns the name of the widget. |
| 40 | * |
| 41 | * @return string |
| 42 | */ |
| 43 | public function getName() |
| 44 | { |
| 45 | return __('VikAppointments - RSS Feeds', 'vikappointments'); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Checks whether the specified user is able to access |
| 50 | * this widget. Allow only super users. |
| 51 | * |
| 52 | * @param mixed $user The user to check. |
| 53 | * |
| 54 | * @return boolean |
| 55 | */ |
| 56 | public function canAccess($user = null) |
| 57 | { |
| 58 | if (!$user instanceof JUser) |
| 59 | { |
| 60 | // get user |
| 61 | $user = JFactory::getUser($user); |
| 62 | } |
| 63 | |
| 64 | // allow super users only |
| 65 | return $user->authorise('core.admin', 'com_vikappointments'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the configuration of the widget. |
| 70 | * |
| 71 | * @return JRegistry A registry of the configuration. |
| 72 | */ |
| 73 | public function getConfig() |
| 74 | { |
| 75 | // obtain dashboard configuration |
| 76 | $config = parent::getConfig(); |
| 77 | |
| 78 | try |
| 79 | { |
| 80 | // inject opt-in date within configuration |
| 81 | $config->set('optin', $this->rss->optedIn($date = true)); |
| 82 | } |
| 83 | catch (Exception $e) |
| 84 | { |
| 85 | // missing opt-in |
| 86 | $config->set('optin', false); |
| 87 | } |
| 88 | |
| 89 | // inject supported channels within configuration |
| 90 | $config->set('channels', $this->rss->getChannels()); |
| 91 | |
| 92 | return $config; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Renders the HTML to display within the contents of the widget. |
| 97 | * |
| 98 | * @param mixed $args A registry of settings. |
| 99 | * |
| 100 | * @return string The HTML to display. |
| 101 | */ |
| 102 | protected function renderWidget($args) |
| 103 | { |
| 104 | try |
| 105 | { |
| 106 | $options = array( |
| 107 | // take a number of feeds equals to the value |
| 108 | // specified from the widget config (5 by default) |
| 109 | 'limit' => $args->get('limit', 5), |
| 110 | ); |
| 111 | |
| 112 | // try to download feeds |
| 113 | $feeds = $this->rss->download($options); |
| 114 | } |
| 115 | catch (Throwable $e) |
| 116 | { |
| 117 | // go ahead silently |
| 118 | $feeds = array(); |
| 119 | } |
| 120 | |
| 121 | // prepare display data |
| 122 | $data = array( |
| 123 | 'config' => $args, |
| 124 | 'widget' => $this, |
| 125 | 'feeds' => $feeds, |
| 126 | ); |
| 127 | |
| 128 | // create layout file |
| 129 | $layout = new JLayoutFile('html.wpdash.rss.widget', null, array('component' => 'com_vikappointments')); |
| 130 | |
| 131 | // render widget content |
| 132 | return $layout->render($data); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns an instance of the form, if supported. |
| 137 | * Implement this method in order to avoid forcing |
| 138 | * its declaration in subclasses. |
| 139 | * |
| 140 | * @return null|JForm |
| 141 | */ |
| 142 | public function getForm() |
| 143 | { |
| 144 | static $form = null; |
| 145 | |
| 146 | if (!$form) |
| 147 | { |
| 148 | // build XML path |
| 149 | $xml = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'forms' . DIRECTORY_SEPARATOR . 'rss.xml'; |
| 150 | |
| 151 | // prepare form options |
| 152 | $options = array( |
| 153 | 'control' => 'jform', |
| 154 | 'client' => 'vikappointments', |
| 155 | ); |
| 156 | |
| 157 | // create form only once |
| 158 | $form = JForm::getInstance($this->getID(), $xml, $options); |
| 159 | } |
| 160 | |
| 161 | return $form; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Renders the HTML to display within the configuration of the widget. |
| 166 | * Implement this method in order to avoid forcing its declaration in subclasses. |
| 167 | * |
| 168 | * @param mixed $args A registry of settings. |
| 169 | * |
| 170 | * @return string The HTML to display. |
| 171 | */ |
| 172 | protected function renderForm($args) |
| 173 | { |
| 174 | $document = JFactory::getDocument(); |
| 175 | |
| 176 | // force 100% width for configuration fieldsets |
| 177 | $document->addStyleDeclaration('#vik_appointments_rss .postbox-container { width: 100% !important; }'); |
| 178 | $document->addStyleDeclaration('#vik_appointments_rss .postbox-header h2 { padding: 6px 12px; }'); |
| 179 | |
| 180 | // let parent renders the configuration starting |
| 181 | // from the specified XML form |
| 182 | return parent::renderForm($args); |
| 183 | } |
| 184 | } |
| 185 |