utils.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * Filter to Run WP REST API v2 Endpoints and Fix double Slash in rest_url
  4. */
  5. //add_filter( 'rest_url_prefix', function () { return 'wp-json/wp/v2'; } );
  6. //add_filter( 'rest_url_prefix', 'fix_json_endpoint' );
  7. //function fix_json_endpoint (){
  8. // $json_endpoint = 'wp-json/wp/v2';
  9. // return rtrim( $json_endpoint, '/' );
  10. //}
  11. //remove_filter('json_dispatch_request', 'json_v1_compatible_dispatch', 10);
  12. //remove_filter('json_endpoints', 'json_v1_compatible_routes', 1000);
  13. /**
  14. * Build path data for current request.
  15. *
  16. * @return string|bool
  17. */
  18. function _s_backbone_get_request_path() {
  19. global $wp_rewrite;
  20. if ( $wp_rewrite->using_permalinks() ) {
  21. global $wp;
  22. // If called too early, bail
  23. if ( ! isset( $wp->request ) ) {
  24. return false;
  25. }
  26. // Determine path for paginated version of current request
  27. if ( false != preg_match( '#' . $wp_rewrite->pagination_base . '/\d+/?$#i', $wp->request ) ) {
  28. $path = preg_replace( '#' . $wp_rewrite->pagination_base . '/\d+$#i', $wp_rewrite->pagination_base . '/%d', $wp->request );
  29. } else {
  30. $path = $wp->request . '/' . $wp_rewrite->pagination_base . '/%d';
  31. }
  32. // Slashes everywhere we need them
  33. if ( 0 !== strpos( $path, '/' ) )
  34. $path = '/' . $path;
  35. $path = user_trailingslashit( $path );
  36. } else {
  37. // Clean up raw $_REQUEST input
  38. $path = array_map( 'sanitize_text_field', $_REQUEST );
  39. $path = array_filter( $path );
  40. $path['paged'] = '%d';
  41. $path = add_query_arg( $path, '/' );
  42. }
  43. return empty( $path ) ? false : $path;
  44. }
  45. /**
  46. * Return query string for current request, prefixed with '?'.
  47. *
  48. * @return string
  49. */
  50. function _s_backbone_get_request_parameters() {
  51. $uri = $_SERVER[ 'REQUEST_URI' ];
  52. $uri = preg_replace( '/^[^?]*(\?.*$)/', '$1', $uri, 1, $count );
  53. if ( $count != 1 ) {
  54. return '';
  55. }
  56. return $uri;
  57. }
  58. /*******************
  59. < PHP 7.3 include file as dom.php
  60. >= PHP 7.3 include file as html_dom.php
  61. **********************/
  62. function til() {
  63. require_once('lib/html_dom.php');
  64. $url = 'https://davidawindham.com/til/index.html';
  65. $html = file_get_html($url);
  66. $i = 0;
  67. foreach ($html->find('ul li ul li') as $li) {
  68. echo $li;
  69. if (++$i == 9) break;
  70. }
  71. }
  72. function pull_til() {
  73. require_once('lib/html_dom.php');
  74. $url = 'https://davidawindham.com/til/index.html';
  75. $html = file_get_html($url);
  76. $i = 0;
  77. foreach ($html->find('ul li ul li') as $li) {
  78. echo $li;
  79. if (++$i == 43) break;
  80. }
  81. }
  82. function pull_til_all() {
  83. require_once('lib/html_dom.php');
  84. $url = 'https://davidawindham.com/til/index.html';
  85. $html = file_get_html($url);
  86. $i = 0;
  87. foreach ($html->find('ul li ul li') as $li) {
  88. echo $li;
  89. }
  90. }
  91. function bookmarks() {
  92. require_once('lib/html_dom.php');
  93. $url = 'https://davidawindham.com/bookmarks/';
  94. $html = file_get_html($url);
  95. $i = 0;
  96. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  97. if (!empty($html)) {
  98. echo $li . '<br />';
  99. if (++$i == 9) break;
  100. }
  101. }
  102. }
  103. function bookmarks_pull() {
  104. require_once('lib/html_dom.php');
  105. $url = 'https://davidawindham.com/bookmarks/';
  106. $html = file_get_html($url);
  107. $i = 0;
  108. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  109. if (!empty($html)) {
  110. echo $li . '<br />';
  111. if (++$i == 27) break;
  112. }
  113. }
  114. }
  115. function bookmarks_all() {
  116. require_once('lib/html_dom.php');
  117. $url = 'https://davidawindham.com/bookmarks/';
  118. $html = file_get_html($url);
  119. $i = 0;
  120. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  121. if (!empty($html)) {
  122. echo $li . '<br />';
  123. }
  124. }
  125. }
  126. function photo() {
  127. require_once('lib/html_dom.php');
  128. $url = 'https://photo.davidwindham.com/#16734437965516';
  129. $html = file_get_html($url);
  130. $i = 0;
  131. foreach ($html->find('.photo .overlay h1') as $li) {
  132. if (!empty($html)) {
  133. echo str_replace('href="/', 'href="https://photo.davidawindham.com/', $li . '&nbsp;');
  134. if (++$i == 18) break;
  135. }
  136. }
  137. }
  138. function work() {
  139. require_once('lib/html_dom.php');
  140. $url = 'https://davidawindham.com/til/notes/work';
  141. $html = file_get_html($url);
  142. $i = 0;
  143. foreach ($html->find('.markdown ul li') as $li) {
  144. if (!empty($html)) {
  145. echo $li;
  146. if (++$i == 3) break;
  147. }
  148. }
  149. }
  150. function todo() {
  151. require_once('lib/html_dom.php');
  152. $url = 'https://davidawindham.com/til/lists/todo';
  153. $html = file_get_html($url);
  154. $i = 0;
  155. foreach ($html->find('.markdown ul li') as $li) {
  156. if (!empty($html)) {
  157. echo $li;
  158. if (++$i == 3) break;
  159. }
  160. }
  161. }
  162. function todone() {
  163. require_once('lib/html_dom.php');
  164. $url = 'https://davidawindham.com/til/lists/todone';
  165. $html = file_get_html($url);
  166. $i = 0;
  167. foreach ($html->find('.markdown ul li') as $li) {
  168. if (!empty($html)) {
  169. echo $li;
  170. if (++$i == 3) break;
  171. }
  172. }
  173. }
  174. function code() {
  175. require_once('lib/html_dom.php');
  176. $url = 'https://code.davidawindham.com/david';
  177. $html = file_get_html($url);
  178. $i = 0;
  179. foreach ($html->find('.item a.name, p.has-emoji, span.time-since') as $li) {
  180. if (!empty($html)) {
  181. //$time = find('.time-since') as $time;
  182. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li . '&nbsp;');
  183. //echo $time . '&nbsp;';
  184. if (++$i == 9) break;
  185. }
  186. }
  187. }
  188. function docs() {
  189. require_once('lib/html_dom.php');
  190. $url = 'https://davidawindham.com/til/docs';
  191. $html = file_get_html($url);
  192. $i = 0;
  193. foreach ($html->find('.markdown ul li') as $li) {
  194. if (!empty($html)) {
  195. echo $li;
  196. if (++$i == 7) break;
  197. }
  198. }
  199. }
  200. function docs_code() {
  201. require_once('lib/html_dom.php');
  202. $url = 'https://code.davidawindham.com/david/til/src/main/docs';
  203. $html = file_get_html($url);
  204. $i = 0;
  205. foreach ($html->find('table tr td') as $li) {
  206. if (!empty($html)) {
  207. //if($i++ == 1) continue;
  208. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  209. if(!($i++ % 3)) {
  210. echo '<br>';
  211. }
  212. if (++$i == 1) break;
  213. }
  214. }
  215. }
  216. function lists() {
  217. require_once('lib/html_dom.php');
  218. $url = 'https://davidawindham.com/til/lists/';
  219. $html = file_get_html($url);
  220. $i = 0;
  221. foreach ($html->find('.markdown ul li') as $li) {
  222. if (!empty($html)) {
  223. echo $li;
  224. if (++$i == 7) break;
  225. }
  226. }
  227. }
  228. function lists_code() {
  229. require_once('lib/html_dom.php');
  230. $url = 'https://code.davidawindham.com/david/til/src/main/lists';
  231. $html = file_get_html($url);
  232. $i = 0;
  233. foreach ($html->find('table tr td') as $li) {
  234. if (!empty($html)) {
  235. //if($i++ == 1) continue;
  236. if (++$i == 2) break;
  237. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  238. if(!($i++ % 3)) {
  239. echo '<br>';
  240. }
  241. }
  242. }
  243. }
  244. function notes() {
  245. require_once('lib/html_dom.php');
  246. $url = 'https://davidawindham.com/til/notes/';
  247. $html = file_get_html($url);
  248. $i = 0;
  249. foreach ($html->find('.markdown ul li') as $li) {
  250. if (!empty($html)) {
  251. echo $li;
  252. if (++$i == 7) break;
  253. }
  254. }
  255. }
  256. function listening() {
  257. require_once('lib/html_dom.php');
  258. $url = 'https://davidawindham.com/studio/music/';
  259. $html = file_get_html($url);
  260. $i = 0;
  261. foreach ($html->find('ul .wtracks li') as $li) {
  262. if (!empty($html)) {
  263. echo $li;
  264. if (++$i == 7) break;
  265. }
  266. }
  267. }
  268. function reading() {
  269. require_once('lib/html_dom.php');
  270. $url = 'https://davidawindham.com/til/lists/now/reading';
  271. $html = file_get_html($url);
  272. $i = 0;
  273. foreach ($html->find('.markdown ul li') as $li) {
  274. if (!empty($html)) {
  275. echo $li;
  276. if (++$i == 7) break;
  277. }
  278. }
  279. }
  280. function learning() {
  281. require_once('lib/html_dom.php');
  282. $url = 'https://davidawindham.com/til/lists/now/learning';
  283. $html = file_get_html($url);
  284. $i = 0;
  285. foreach ($html->find('.markdown ul li') as $li) {
  286. if (!empty($html)) {
  287. echo $li;
  288. if (++$i == 7) break;
  289. }
  290. }
  291. }
  292. function playing() {
  293. require_once('lib/html_dom.php');
  294. $url = 'https://davidawindham.com/til/lists/now/playing';
  295. $html = file_get_html($url);
  296. $i = 0;
  297. foreach ($html->find('.markdown ul li') as $li) {
  298. if (!empty($html)) {
  299. echo $li;
  300. if (++$i == 7) break;
  301. }
  302. }
  303. }
  304. function watching() {
  305. require_once('lib/html_dom.php');
  306. $url = 'https://davidawindham.com/til/lists/now/watching';
  307. $html = file_get_html($url);
  308. $i = 0;
  309. foreach ($html->find('.markdown ul li') as $li) {
  310. if (!empty($html)) {
  311. echo $li;
  312. if (++$i == 7) break;
  313. }
  314. }
  315. }