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

https://gitlab.com/hunt9310/ras · PHP · 332 lines · 180 code · 43 blank · 109 comment · 33 complexity · e2b05e7d12e6d9ebff9dfebdfa3d78cd MD5 · raw file

  1. <?php
  2. require_once( dirname(__FILE__).'/icwp-data-processor.php' );
  3. if ( !class_exists('ICWP_WPTB_WpFunctions_V4') ):
  4. class ICWP_WPTB_WpFunctions_V4 {
  5. /**
  6. * @var ICWP_WPTB_WpFunctions_V4
  7. */
  8. protected static $oInstance = NULL;
  9. /**
  10. * @return ICWP_WPTB_WpFunctions_V4
  11. */
  12. public static function GetInstance() {
  13. if ( is_null( self::$oInstance ) ) {
  14. self::$oInstance = new self();
  15. }
  16. return self::$oInstance;
  17. }
  18. /**
  19. * @var string
  20. */
  21. protected $m_sWpVersion;
  22. /**
  23. * @var boolean
  24. */
  25. protected $fIsMultisite;
  26. public function __construct() {}
  27. /**
  28. * @param string $sPluginFile
  29. * @return boolean|stdClass
  30. */
  31. public function getIsPluginUpdateAvailable( $sPluginFile ) {
  32. $aUpdates = $this->getWordpressUpdates();
  33. if ( empty( $aUpdates ) ) {
  34. return false;
  35. }
  36. if ( isset( $aUpdates[ $sPluginFile ] ) ) {
  37. return $aUpdates[ $sPluginFile ];
  38. }
  39. return false;
  40. }
  41. /**
  42. * @param $sPluginFile
  43. * @return mixed
  44. */
  45. public function getPluginUpgradeLink( $sPluginFile ) {
  46. $sUrl = self_admin_url( 'update.php' ) ;
  47. $aQueryArgs = array(
  48. 'action' => 'upgrade-plugin',
  49. 'plugin' => urlencode( $sPluginFile ),
  50. '_wpnonce' => wp_create_nonce( 'upgrade-plugin_' . $sPluginFile )
  51. );
  52. return add_query_arg( $aQueryArgs, $sUrl );
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function getWordpressUpdates() {
  58. $oCurrent = $this->getTransient( 'update_plugins' );
  59. return ( is_object( $oCurrent ) && isset( $oCurrent->response ) ) ? $oCurrent->response : array();
  60. }
  61. /**
  62. * The full plugin file to be upgraded.
  63. *
  64. * @param string $sPluginFile
  65. * @return boolean
  66. */
  67. public function doPluginUpgrade( $sPluginFile ) {
  68. if ( !$this->getIsPluginUpdateAvailable( $sPluginFile )
  69. || ( isset( $GLOBALS['pagenow'] ) && $GLOBALS['pagenow'] == 'update.php' ) ) {
  70. return true;
  71. }
  72. $sUrl = $this->getPluginUpgradeLink( $sPluginFile );
  73. wp_redirect( $sUrl );
  74. exit();
  75. }
  76. /**
  77. * @param string $sKey
  78. * @return object
  79. */
  80. protected function getTransient( $sKey ) {
  81. // TODO: Handle multisite
  82. if ( version_compare( $this->getWordpressVersion(), '2.7.9', '<=' ) ) {
  83. return get_option( $sKey );
  84. }
  85. if ( function_exists( 'get_site_transient' ) ) {
  86. $mResult = get_site_transient( $sKey );
  87. if ( empty( $mResult ) ) {
  88. remove_all_filters( 'pre_site_transient_'.$sKey );
  89. $mResult = get_site_transient( $sKey );
  90. }
  91. return $mResult;
  92. }
  93. if ( version_compare( $this->getWordpressVersion(), '2.9.9', '<=' ) ) {
  94. return apply_filters( 'transient_'.$sKey, get_option( '_transient_'.$sKey ) );
  95. }
  96. return apply_filters( 'site_transient_'.$sKey, get_option( '_site_transient_'.$sKey ) );
  97. }
  98. /**
  99. * @return string
  100. */
  101. public function getWordpressVersion() {
  102. global $wp_version;
  103. if ( empty( $this->m_sWpVersion ) ) {
  104. $sVersionFile = ABSPATH.WPINC.'/version.php';
  105. $sVersionContents = file_get_contents( $sVersionFile );
  106. if ( preg_match( '/wp_version\s=\s\'([^(\'|")]+)\'/i', $sVersionContents, $aMatches ) ) {
  107. $this->m_sWpVersion = $aMatches[1];
  108. }
  109. }
  110. return empty( $this->m_sWpVersion )? $wp_version : $this->m_sWpVersion;
  111. }
  112. /**
  113. * @param array $aQueryParams
  114. */
  115. public function redirectToLogin( $aQueryParams = array() ) {
  116. $sLoginUrl = site_url() . '/wp-login.php';
  117. $this->doRedirect( $sLoginUrl, $aQueryParams );
  118. exit();
  119. }
  120. /**
  121. * @param $aQueryParams
  122. */
  123. public function redirectToAdmin( $aQueryParams = array() ) {
  124. $this->doRedirect( is_multisite()? get_admin_url() : admin_url(), $aQueryParams );
  125. }
  126. /**
  127. * @param $aQueryParams
  128. */
  129. public function redirectToHome( $aQueryParams = array() ) {
  130. $this->doRedirect( home_url(), $aQueryParams );
  131. }
  132. /**
  133. * @param $sUrl
  134. * @param $aQueryParams
  135. */
  136. public function doRedirect( $sUrl, $aQueryParams = array() ) {
  137. $sUrl = empty( $aQueryParams ) ? $sUrl : add_query_arg( $aQueryParams, $sUrl ) ;
  138. wp_safe_redirect( $sUrl );
  139. exit();
  140. }
  141. /**
  142. * @return string
  143. */
  144. public function getCurrentPage() {
  145. global $pagenow;
  146. return $pagenow;
  147. }
  148. /**
  149. * @param string
  150. * @return string
  151. */
  152. public function getIsCurrentPage( $sPage ) {
  153. return $sPage == $this->getCurrentPage();
  154. }
  155. /**
  156. * @return bool
  157. */
  158. public function getIsLoginRequest() {
  159. $oDp = $this->loadDataProcessor();
  160. return
  161. $oDp->GetIsRequestPost()
  162. && $this->getIsCurrentPage( 'wp-login.php' )
  163. && !is_null( $oDp->FetchPost( 'log' ) )
  164. && !is_null( $oDp->FetchPost( 'pwd' ) );
  165. }
  166. /**
  167. * @return string
  168. */
  169. public function getSiteName() {
  170. return function_exists( 'get_bloginfo' )? get_bloginfo('name') : 'WordPress Site';
  171. }
  172. /**
  173. * @return string
  174. */
  175. public function getSiteAdminEmail() {
  176. return function_exists( 'get_bloginfo' )? get_bloginfo('admin_email') : '';
  177. }
  178. /**
  179. * @return boolean
  180. */
  181. public function getIsAjax() {
  182. return defined( 'DOING_AJAX' ) && DOING_AJAX;
  183. }
  184. /**
  185. * @param string $sRedirectUrl
  186. */
  187. public function logoutUser( $sRedirectUrl = '' ) {
  188. empty( $sRedirectUrl ) ? wp_logout() : wp_logout_url( $sRedirectUrl );
  189. }
  190. /**
  191. * @return bool
  192. */
  193. public function isMultisite() {
  194. if ( !isset( $this->fIsMultisite ) ) {
  195. $this->fIsMultisite = function_exists( 'is_multisite' ) && is_multisite();
  196. }
  197. return $this->fIsMultisite;
  198. }
  199. /**
  200. * @param string $sKey
  201. * @param $sValue
  202. * @return mixed
  203. */
  204. public function addOption( $sKey, $sValue ) {
  205. return $this->isMultisite() ? add_site_option( $sKey, $sValue ) : add_option( $sKey, $sValue );
  206. }
  207. /**
  208. * @param string $sKey
  209. * @param $sValue
  210. * @return mixed
  211. */
  212. public function updateOption( $sKey, $sValue ) {
  213. return $this->isMultisite() ? update_site_option( $sKey, $sValue ) : update_option( $sKey, $sValue );
  214. }
  215. /**
  216. * @param string $sKey
  217. * @param mixed $mDefault
  218. * @return mixed
  219. */
  220. public function getOption( $sKey, $mDefault = false ) {
  221. return $this->isMultisite() ? get_site_option( $sKey, $mDefault ) : get_option( $sKey, $mDefault );
  222. }
  223. /**
  224. * @param string $sKey
  225. * @return mixed
  226. */
  227. public function deleteOption( $sKey ) {
  228. return $this->isMultisite() ? delete_site_option( $sKey ) : delete_option( $sKey );
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getCurrentWpAdminPage() {
  234. $oDp = $this->loadDataProcessor();
  235. $sScript = $oDp->FetchServer( 'SCRIPT_NAME' );
  236. if ( empty( $sScript ) ) {
  237. $sScript = $oDp->FetchServer( 'PHP_SELF' );
  238. }
  239. if ( is_admin() && !empty( $sScript ) && basename( $sScript ) == 'admin.php' ) {
  240. $sCurrentPage = $oDp->FetchGet( 'page' );
  241. }
  242. return empty( $sCurrentPage ) ? '' : $sCurrentPage;
  243. }
  244. /**
  245. * @return null|WP_User
  246. */
  247. public function getCurrentWpUser() {
  248. if ( is_user_logged_in() ) {
  249. $oUser = wp_get_current_user();
  250. if ( is_object( $oUser ) && $oUser instanceof WP_User ) {
  251. return $oUser;
  252. }
  253. }
  254. return null;
  255. }
  256. /**
  257. * @param $sUsername
  258. */
  259. public function setUserLoggedIn( $sUsername ) {
  260. $oUser = version_compare( $this->getWordpressVersion(), '2.8.0', '<' )? get_userdatabylogin( $sUsername ) : get_user_by( 'login', $sUsername );
  261. wp_clear_auth_cookie();
  262. wp_set_current_user ( $oUser->ID, $oUser->user_login );
  263. wp_set_auth_cookie ( $oUser->ID, true );
  264. do_action( 'wp_login', $oUser->user_login, $oUser );
  265. }
  266. /**
  267. * @return ICWP_WPTB_DataProcessor
  268. */
  269. public function loadDataProcessor() {
  270. if ( !class_exists('ICWP_WPTB_DataProcessor') ) {
  271. require_once( dirname(__FILE__).'/icwp-data-processor.php' );
  272. }
  273. return ICWP_WPTB_DataProcessor::GetInstance();
  274. }
  275. }
  276. endif;
  277. if ( !class_exists('ICWP_WPTB_WpFunctions') ):
  278. class ICWP_WPTB_WpFunctions extends ICWP_WPTB_WpFunctions_V4 {
  279. /**
  280. * @return ICWP_WPTB_WpFunctions
  281. */
  282. public static function GetInstance() {
  283. if ( is_null( self::$oInstance ) ) {
  284. self::$oInstance = new self();
  285. }
  286. return self::$oInstance;
  287. }
  288. }
  289. endif;