factory.php
213 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.factory |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 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 | * Abstract Factory pattern. |
| 16 | * |
| 17 | * @since 10.0 |
| 18 | */ |
| 19 | abstract class JFactory |
| 20 | { |
| 21 | /** |
| 22 | * Application CMS adapter instance. |
| 23 | * |
| 24 | * @var JApplication |
| 25 | */ |
| 26 | private static $application = null; |
| 27 | |
| 28 | /** |
| 29 | * Language adapter instance. |
| 30 | * |
| 31 | * @var JLanguage |
| 32 | */ |
| 33 | private static $language = null; |
| 34 | |
| 35 | /** |
| 36 | * Document adapter instance. |
| 37 | * |
| 38 | * @var JDocument |
| 39 | */ |
| 40 | private static $document = null; |
| 41 | |
| 42 | /** |
| 43 | * Access point to retrieve the current database instance. |
| 44 | * |
| 45 | * @return JDatabase Global database adapter. |
| 46 | */ |
| 47 | public static function getDbo() |
| 48 | { |
| 49 | JLoader::import('adapter.database.database'); |
| 50 | |
| 51 | global $wpdb; |
| 52 | |
| 53 | return JDatabase::getInstance($wpdb); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get an application object. |
| 58 | * Returns the global application object, only creating it if it doesn't already exist. |
| 59 | * |
| 60 | * @return JApplication The application adapter. |
| 61 | */ |
| 62 | public static function getApplication() |
| 63 | { |
| 64 | if (static::$application === null) |
| 65 | { |
| 66 | JLoader::import('adapter.application.application'); |
| 67 | |
| 68 | static::$application = new JApplication(); |
| 69 | } |
| 70 | |
| 71 | return static::$application; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get a language object. |
| 76 | * Returns the global language object, only creating it if it doesn't already exist. |
| 77 | * |
| 78 | * @return JLanguage The language adapter. |
| 79 | */ |
| 80 | public static function getLanguage() |
| 81 | { |
| 82 | if (static::$language === null) |
| 83 | { |
| 84 | JLoader::import('adapter.language.language'); |
| 85 | |
| 86 | static::$language = JLanguage::getInstance(); |
| 87 | } |
| 88 | |
| 89 | return static::$language; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get a session object. |
| 94 | * Returns the global session object, only creating it if it doesn't already exist. |
| 95 | * |
| 96 | * @param array $options An array containing session options (@unused). |
| 97 | * |
| 98 | * @return JSession The session adapter. |
| 99 | */ |
| 100 | public static function getSession(array $options = array()) |
| 101 | { |
| 102 | JLoader::import('adapter.session.session'); |
| 103 | JLoader::import('adapter.session.handler'); |
| 104 | |
| 105 | // attempt to start the session before using it |
| 106 | JSessionHandler::start(); |
| 107 | |
| 108 | return JSession::getInstance(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get a document object. |
| 113 | * Returns the global document object, only creating it if it doesn't already exist. |
| 114 | * |
| 115 | * @return JDocument The document adapter. |
| 116 | */ |
| 117 | public static function getDocument() |
| 118 | { |
| 119 | if (static::$document === null) |
| 120 | { |
| 121 | JLoader::import('adapter.application.document'); |
| 122 | |
| 123 | static::$document = new JDocument(); |
| 124 | } |
| 125 | |
| 126 | return static::$document; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get a user object. |
| 131 | * Returns the global user object, only creating it if it doesn't already exist. |
| 132 | * |
| 133 | * @param integer $id The primary key value of the user to load. |
| 134 | * |
| 135 | * @return JUser The user adapter. |
| 136 | */ |
| 137 | public static function getUser($id = null) |
| 138 | { |
| 139 | JLoader::import('adapter.user.user'); |
| 140 | |
| 141 | return JUser::getInstance($id); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Creates a new instance of the specified editor. |
| 146 | * |
| 147 | * @param string $name The editor name type. |
| 148 | * |
| 149 | * @return JEditor The instance of the editor. |
| 150 | */ |
| 151 | public static function getEditor($name = null) |
| 152 | { |
| 153 | JLoader::import('adapter.editor.editor'); |
| 154 | |
| 155 | if (is_null($name)) |
| 156 | { |
| 157 | $name = JFactory::getApplication()->get('editor'); |
| 158 | } |
| 159 | |
| 160 | return JEditor::getInstance($name); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Return the JDate object. |
| 165 | * |
| 166 | * @param mixed $time The initial time for the JDate object |
| 167 | * @param mixed $tzOffset The timezone offset. |
| 168 | * |
| 169 | * @return JDate The date object. |
| 170 | */ |
| 171 | public static function getDate($time = 'now', $tzOffset = null) |
| 172 | { |
| 173 | JLoader::import('adapter.date.date'); |
| 174 | |
| 175 | return JDate::getInstance($time, $tzOffset); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get a mailer object. |
| 180 | * |
| 181 | * Returns the global JMail object, only creating it if it doesn't already exist. |
| 182 | * |
| 183 | * @return JMail The mail object. |
| 184 | */ |
| 185 | public static function getMailer() |
| 186 | { |
| 187 | JLoader::import('adapter.mail.mail'); |
| 188 | |
| 189 | /** |
| 190 | * Always return a new instance of JMail without caching it. |
| 191 | * This avoids having a JMail instance already filled-in when |
| 192 | * this method is called more than once. |
| 193 | * |
| 194 | * @since 10.1.10 |
| 195 | */ |
| 196 | return new JMail; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the configuration registry. |
| 201 | * |
| 202 | * @return JConfig The configuration object. |
| 203 | * |
| 204 | * @since 10.1.4 |
| 205 | */ |
| 206 | public static function getConfig() |
| 207 | { |
| 208 | JLoader::import('adapter.config.config'); |
| 209 | |
| 210 | return new JConfig; |
| 211 | } |
| 212 | } |
| 213 |