PageRenderTime 44ms CodeModel.GetById 6ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/wordpress-bootstrap-css/src/icwp-wpfunctions.php

https://github.com/bhanukolli/WP-Blog
PHP | 318 lines | 163 code | 42 blank | 113 comment | 30 complexity | 312e164cb1a92dfefa2c5c9c0afba929 MD5 | raw file
  1<?php
  2/**
  3 * Copyright (c) 2014 iControlWP <support@icontrolwp.com>
  4 * All rights reserved.
  5 *
  6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  7 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  8 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  9 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 10 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 11 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 12 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 13 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 15 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 16 */
 17
 18require_once( dirname(__FILE__).'/icwp-data-processor.php' );
 19
 20if ( !class_exists('ICWP_WPTB_WpFunctions_V4') ):
 21
 22	class ICWP_WPTB_WpFunctions_V4 {
 23
 24		/**
 25		 * @var ICWP_WPTB_WpFunctions_V4
 26		 */
 27		protected static $oInstance = NULL;
 28
 29		/**
 30		 * @return ICWP_WPTB_WpFunctions_V4
 31		 */
 32		public static function GetInstance() {
 33			if ( is_null( self::$oInstance ) ) {
 34				self::$oInstance = new self();
 35			}
 36			return self::$oInstance;
 37		}
 38
 39		/**
 40		 * @var string
 41		 */
 42		protected $m_sWpVersion;
 43
 44		/**
 45		 * @var boolean
 46		 */
 47		protected $fIsMultisite;
 48
 49		public function __construct() {}
 50
 51		/**
 52		 * @param string $insPluginFile
 53		 * @return boolean|stdClass
 54		 */
 55		public function getIsPluginUpdateAvailable( $insPluginFile ) {
 56			$aUpdates = $this->getWordpressUpdates();
 57			if ( empty( $aUpdates ) ) {
 58				return false;
 59			}
 60			if ( isset( $aUpdates[ $insPluginFile ] ) ) {
 61				return $aUpdates[ $insPluginFile ];
 62			}
 63			return false;
 64		}
 65
 66		public function getPluginUpgradeLink( $insPluginFile ) {
 67			$sUrl = self_admin_url( 'update.php' ) ;
 68			$aQueryArgs = array(
 69				'action' 	=> 'upgrade-plugin',
 70				'plugin'	=> urlencode( $insPluginFile ),
 71				'_wpnonce'	=> wp_create_nonce( 'upgrade-plugin_' . $insPluginFile )
 72			);
 73			return add_query_arg( $aQueryArgs, $sUrl );
 74		}
 75
 76		public function getWordpressUpdates() {
 77			$oCurrent = $this->getTransient( 'update_plugins' );
 78			return $oCurrent->response;
 79		}
 80
 81		/**
 82		 * The full plugin file to be upgraded.
 83		 *
 84		 * @param string $insPluginFile
 85		 * @return boolean
 86		 */
 87		public function doPluginUpgrade( $insPluginFile ) {
 88
 89			if ( !$this->getIsPluginUpdateAvailable($insPluginFile)
 90				|| ( isset( $GLOBALS['pagenow'] ) && $GLOBALS['pagenow'] == 'update.php' ) ) {
 91				return true;
 92			}
 93			$sUrl = $this->getPluginUpgradeLink( $insPluginFile );
 94			wp_redirect( $sUrl );
 95			exit();
 96		}
 97
 98		/**
 99		 * @param string $insKey
100		 * @return object
101		 */
102		protected function getTransient( $insKey ) {
103
104			// TODO: Handle multisite
105
106			if ( version_compare( $this->getWordpressVersion(), '2.7.9', '<=' ) ) {
107				return get_option( $insKey );
108			}
109
110			if ( function_exists( 'get_site_transient' ) ) {
111				return get_site_transient( $insKey );
112			}
113
114			if ( version_compare( $this->getWordpressVersion(), '2.9.9', '<=' ) ) {
115				return apply_filters( 'transient_'.$insKey, get_option( '_transient_'.$insKey ) );
116			}
117
118			return apply_filters( 'site_transient_'.$insKey, get_option( '_site_transient_'.$insKey ) );
119		}
120
121		/**
122		 * @return string
123		 */
124		public function getWordpressVersion() {
125			global $wp_version;
126
127			if ( empty( $this->m_sWpVersion ) ) {
128				$sVersionFile = ABSPATH.WPINC.'/version.php';
129				$sVersionContents = file_get_contents( $sVersionFile );
130
131				if ( preg_match( '/wp_version\s=\s\'([^(\'|")]+)\'/i', $sVersionContents, $aMatches ) ) {
132					$this->m_sWpVersion = $aMatches[1];
133				}
134			}
135			return empty( $this->m_sWpVersion )? $wp_version : $this->m_sWpVersion;
136		}
137
138		/**
139		 * @param array $aQueryParams
140		 */
141		public function redirectToLogin( $aQueryParams = array() ) {
142			$sLoginUrl = site_url() . '/wp-login.php';
143			$this->doRedirect( $sLoginUrl, $aQueryParams );
144			exit();
145		}
146		/**
147		 * @param $aQueryParams
148		 */
149		public function redirectToAdmin( $aQueryParams = array() ) {
150			$this->doRedirect( is_multisite()? get_admin_url() : admin_url(), $aQueryParams );
151		}
152		/**
153		 * @param $aQueryParams
154		 */
155		public function redirectToHome( $aQueryParams = array() ) {
156			$this->doRedirect( home_url(), $aQueryParams );
157		}
158
159		/**
160		 * @param $sUrl
161		 * @param $aQueryParams
162		 */
163		public function doRedirect( $sUrl, $aQueryParams = array() ) {
164			$sUrl = empty( $aQueryParams ) ? $sUrl : add_query_arg( $aQueryParams, $sUrl ) ;
165			wp_safe_redirect( $sUrl );
166			exit();
167		}
168
169		/**
170		 * @return string
171		 */
172		public function getCurrentPage() {
173			global $pagenow;
174			return $pagenow;
175		}
176
177		/**
178		 * @param string
179		 * @return string
180		 */
181		public function getIsCurrentPage( $sPage ) {
182			return $sPage == $this->getCurrentPage();
183		}
184
185		/**
186		 * @return bool
187		 */
188		public function getIsLoginRequest() {
189			return ICWP_WPTB_DataProcessor::GetIsRequestPost()
190			&& $this->getIsCurrentPage('wp-login.php')
191			&& !is_null( ICWP_WPTB_DataProcessor::FetchPost('log') )
192			&& !is_null( ICWP_WPTB_DataProcessor::FetchPost('pwd') );
193		}
194
195		/**
196		 * @return string
197		 */
198		public function getSiteName() {
199			return function_exists( 'get_bloginfo' )? get_bloginfo('name') : 'WordPress Site';
200		}
201		/**
202		 * @return string
203		 */
204		public function getSiteAdminEmail() {
205			return function_exists( 'get_bloginfo' )? get_bloginfo('admin_email') : '';
206		}
207
208		/**
209		 * @return boolean
210		 */
211		public function getIsAjax() {
212			return defined( 'DOING_AJAX' ) && DOING_AJAX;
213		}
214
215		/**
216		 * @param string $sRedirectUrl
217		 */
218		public function logoutUser( $sRedirectUrl = '' ) {
219			empty( $sRedirectUrl ) ? wp_logout() : wp_logout_url( $sRedirectUrl );
220		}
221
222		/**
223		 * @return bool
224		 */
225		public function isMultisite() {
226			if ( !isset( $this->fIsMultisite ) ) {
227				$this->fIsMultisite = function_exists( 'is_multisite' ) && is_multisite();
228			}
229			return $this->fIsMultisite;
230		}
231
232		/**
233		 * @param string $sKey
234		 * @param $sValue
235		 * @return mixed
236		 */
237		public function addOption( $sKey, $sValue ) {
238			return $this->isMultisite() ? add_site_option( $sKey, $sValue ) : add_option( $sKey, $sValue );
239		}
240
241		/**
242		 * @param string $sKey
243		 * @param $sValue
244		 * @return mixed
245		 */
246		public function updateOption( $sKey, $sValue ) {
247			return $this->isMultisite() ? update_site_option( $sKey, $sValue ) : update_option( $sKey, $sValue );
248		}
249
250		/**
251		 * @param string $sKey
252		 * @param mixed $mDefault
253		 * @return mixed
254		 */
255		public function getOption( $sKey, $mDefault = false ) {
256			return $this->isMultisite() ? get_site_option( $sKey, $mDefault ) : get_option( $sKey, $mDefault );
257		}
258
259		/**
260		 * @param string $sKey
261		 * @return mixed
262		 */
263		public function deleteOption( $sKey ) {
264			return $this->isMultisite() ? delete_site_option( $sKey ) : delete_option( $sKey );
265		}
266
267		/**
268		 */
269		public function getCurrentWpAdminPage() {
270			$sScript = isset( $_SERVER['SCRIPT_NAME'] )? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF'];
271			if ( is_admin() && !empty( $sScript ) && basename( $sScript ) == 'admin.php' && isset( $_GET['page'] ) ) {
272				$sCurrentPage = $_GET['page'];
273			}
274			return empty($sCurrentPage)? '' : $sCurrentPage;
275		}
276
277		/**
278		 * @return null|WP_User
279		 */
280		public function getCurrentWpUser() {
281			if ( is_user_logged_in() ) {
282				$oUser = wp_get_current_user();
283				if ( is_object( $oUser ) && $oUser instanceof WP_User ) {
284					return $oUser;
285				}
286			}
287			return null;
288		}
289
290		/**
291		 * @param $sUsername
292		 */
293		public function setUserLoggedIn( $sUsername ) {
294			$oUser = version_compare( $this->getWordpressVersion(), '2.8.0', '<' )? get_userdatabylogin( $sUsername ) : get_user_by( 'login', $sUsername );
295
296			wp_clear_auth_cookie();
297			wp_set_current_user ( $oUser->ID, $oUser->user_login );
298			wp_set_auth_cookie  ( $oUser->ID, true );
299			do_action( 'wp_login', $oUser->user_login, $oUser );
300		}
301	}
302endif;
303
304
305if ( !class_exists('ICWP_WPTB_WpFunctions') ):
306
307	class ICWP_WPTB_WpFunctions extends ICWP_WPTB_WpFunctions_V4 {
308		/**
309		 * @return ICWP_WPTB_WpFunctions
310		 */
311		public static function GetInstance() {
312			if ( is_null( self::$oInstance ) ) {
313				self::$oInstance = new self();
314			}
315			return self::$oInstance;
316		}
317	}
318endif;