Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. <?php
  2.  
  3. class ffUrlRewriter extends ffBasicObject {
  4. /**********************************************************************************************************************/
  5. /* OBJECTS
  6. /**********************************************************************************************************************/
  7. /**
  8. * @var ffWPLayer
  9. */
  10. private $_WPLayer = null;
  11.  
  12. /**
  13. * @var ffRequest
  14. */
  15. private $_request = null;
  16. /**********************************************************************************************************************/
  17. /* PRIVATE VARIABLES
  18. /**********************************************************************************************************************/
  19. private $_siteUrl = null;
  20.  
  21. private $_pathUrl = null;
  22.  
  23. private $_queryParams = null;
  24.  
  25. private $_urlHasBeenDisassembled = false;
  26.  
  27. private $_url = null;
  28. /**********************************************************************************************************************/
  29. /* CONSTRUCT
  30. /**********************************************************************************************************************/
  31. public function __construct() {
  32. $this->_setWPLayer( ffContainer()->getWPLayer() );
  33. $this->_setRequest( ffContainer()->getRequest() );
  34. }
  35. /**********************************************************************************************************************/
  36. /* PUBLIC FUNCTIONS
  37. /**********************************************************************************************************************/
  38. public function reset() {
  39. $this->_siteUrl = null;
  40. $this->_pathUrl = null;
  41. $this->_queryParams = null;
  42. $this->_urlHasBeenDisassembled = false;
  43. $this->_url = null;
  44.  
  45. return $this;
  46. }
  47.  
  48. public function setSiteUrl() {
  49. $this->setUrl( $this->_getWPLayer()->get_site_url() );
  50.  
  51. return $this;
  52. }
  53.  
  54.  
  55. public function setUrl( $url ) {
  56. $this->_setUrl( $url );
  57.  
  58. return $this;
  59. }
  60.  
  61. public function getCurrentUrl() {
  62. return $this->_currentUrl();
  63. }
  64.  
  65. public function getNewUrl() {
  66. $this->_disassembleUrl();
  67.  
  68. return $this->_assembleUrl();
  69. }
  70.  
  71. public function addQueryParameter( $name, $value ) {
  72. $this->_disassembleUrl();
  73.  
  74. $this->_queryParams[ $name ] = $value;
  75.  
  76. return $this;
  77. }
  78.  
  79. public function removeQueryParameter( $name ) {
  80. $this->_disassembleUrl();
  81. unset( $this->_queryParams[ $name ] );
  82.  
  83. return $this;
  84. }
  85. /**********************************************************************************************************************/
  86. /* PUBLIC PROPERTIES
  87. /**********************************************************************************************************************/
  88.  
  89.  
  90.  
  91. /**********************************************************************************************************************/
  92. /* PRIVATE FUNCTIONS
  93. /**********************************************************************************************************************/
  94. private function _assembleUrl() {
  95. $base = $this->_getWPLayer()->trailingslashit($this->_siteUrl);
  96.  
  97. $path = '';
  98. if( is_array( $this->_pathUrl ) ) {
  99. $path = implode('/', $this->_pathUrl );
  100. }
  101.  
  102. $query = '';
  103.  
  104. if( is_array( $this->_queryParams ) ) {
  105. $query = http_build_query( $this->_queryParams );
  106. }
  107.  
  108. if( !empty( $query ) ) {
  109. $query = '?' .$query;
  110. }
  111.  
  112. $fullUrl = $base . $path . $query;
  113.  
  114. return $fullUrl;
  115. }
  116.  
  117. private function _disassembleUrl() {
  118. if( $this->_urlHasBeenDisassembled == true ) {
  119. return false;
  120. }
  121.  
  122. // site url == localhost/wordpress
  123. if( $this->_url == null ) {
  124. $this->_siteUrl = $this->_getWPLayer()->get_site_url();
  125. } else {
  126. $fullUrl = $this->_url;
  127.  
  128. $this->_siteUrl = strstr( $fullUrl, '?', true);
  129. }
  130.  
  131.  
  132.  
  133. // currentUrl = something like localhost/wordpress/wp-admin/tools.php?page=import&someVariable=xx
  134. $currentUrl = $this->_currentUrl();
  135.  
  136.  
  137.  
  138. // currentUrlWithoutsite = wp-admin/tools.php?page=import&someVariable=xx
  139. $currentUrlWithoutSite = str_replace( $this->_siteUrl, '', $currentUrl );
  140.  
  141.  
  142. $urlParsed = parse_url( $currentUrlWithoutSite);
  143.  
  144.  
  145. // path = wp-admin/tools.php
  146. if( isset( $urlParsed['path'] ) ) {
  147. $pathSplitted = explode('/', ltrim($urlParsed['path'],'/') );
  148.  
  149. $this->_pathUrl = $pathSplitted;
  150. }
  151. // query = page=import&someVariable=xx
  152. if( isset( $urlParsed['query'] ) ) {
  153. $query = $urlParsed['query'];
  154.  
  155. $queryParsed = array();
  156.  
  157. parse_str( $query, $queryParsed );
  158.  
  159. $this->_queryParams = $queryParsed;
  160. }
  161.  
  162. $this->_urlHasBeenDisassembled = true;
  163. }
  164.  
  165. private function _currentUrl() {
  166.  
  167. if( $this->_getUrl() != null ) {
  168. return $this->_getUrl();
  169. }
  170.  
  171. $pageURL = 'http';
  172. $request = $this->_getRequest();
  173.  
  174. if ( $request->server('HTTPS') == 'on' ) {
  175. $pageURL .= "s";
  176. }
  177. $pageURL .= "://";
  178.  
  179.  
  180. // var_Dump( $request->server('SERVER_POST') );
  181.  
  182. if ( $request->server('SERVER_PORT') != 80 ){
  183. $pageURL .=
  184. $request->server('SERVER_NAME') . ':' . $request->server('SERVER_PORT') . $request->server('REQUEST_URI');
  185. }
  186. else {
  187. $pageURL .= $request->server('SERVER_NAME') . $request->server('REQUEST_URI');
  188. }
  189.  
  190. return $pageURL;
  191. }
  192.  
  193. public function getServerName( $addProtocol = false, $addPort = false ) {
  194. $request = $this->_getRequest();
  195. $serverName = '';
  196.  
  197. if( $addProtocol ) {
  198.  
  199. $serverName = 'http';
  200. $request = $this->_getRequest();
  201.  
  202. if ( $request->server('HTTPS') == 'on' ) {
  203. $serverName = 'https';
  204. }
  205. $serverName .= "://";
  206.  
  207. }
  208.  
  209. $serverName .= $request->server('SERVER_NAME');
  210.  
  211. if( $addPort && $request->server('SERVER_PORT') != 80 ) {
  212. $serverName .= ':' . $request->server('SERVER_PORT');
  213. }
  214.  
  215. return $serverName;
  216. }
  217. /**********************************************************************************************************************/
  218. /* PRIVATE GETTERS & SETTERS
  219. /**********************************************************************************************************************/
  220. /**
  221. * @return ffWPLayer
  222. */
  223. private function _getWPLayer()
  224. {
  225. return $this->_WPLayer;
  226. }
  227.  
  228. /**
  229. * @param ffWPLayer $WPLayer
  230. */
  231. private function _setWPLayer($WPLayer)
  232. {
  233. $this->_WPLayer = $WPLayer;
  234. }
  235.  
  236. /**
  237. * @return ffRequest
  238. */
  239. private function _getRequest()
  240. {
  241. return $this->_request;
  242. }
  243.  
  244. /**
  245. * @param ffRequest $request
  246. */
  247. private function _setRequest($request)
  248. {
  249. $this->_request = $request;
  250. }
  251.  
  252. /**
  253. * @return null
  254. */
  255. private function _getUrl()
  256. {
  257. return $this->_url;
  258. }
  259.  
  260. /**
  261. * @param null $url
  262. */
  263. private function _setUrl($url)
  264. {
  265. $this->_url = $url;
  266. }
  267.  
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement