utils.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 todo() {
  139. require_once('lib/html_dom.php');
  140. $url = 'https://davidawindham.com/til/lists/todo';
  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->childNodes(0);
  146. if (++$i == 9) break;
  147. }
  148. }
  149. }
  150. function code() {
  151. require_once('lib/html_dom.php');
  152. $url = 'https://code.davidawindham.com/david';
  153. $html = file_get_html($url);
  154. $i = 0;
  155. foreach ($html->find('.item a.name, p.has-emoji, span.time-since') as $li) {
  156. if (!empty($html)) {
  157. //$time = find('.time-since') as $time;
  158. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li . '&nbsp;');
  159. //echo $time . '&nbsp;';
  160. if (++$i == 9) break;
  161. }
  162. }
  163. }
  164. function docs() {
  165. require_once('lib/html_dom.php');
  166. $url = 'https://davidawindham.com/til/docs';
  167. $html = file_get_html($url);
  168. $i = 0;
  169. foreach ($html->find('.markdown ul li') as $li) {
  170. if (!empty($html)) {
  171. echo $li;
  172. if (++$i == 7) break;
  173. }
  174. }
  175. }
  176. function docs_code() {
  177. require_once('lib/html_dom.php');
  178. $url = 'https://code.davidawindham.com/david/til/src/main/docs';
  179. $html = file_get_html($url);
  180. $i = 0;
  181. foreach ($html->find('table tr td') as $li) {
  182. if (!empty($html)) {
  183. //if($i++ == 1) continue;
  184. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  185. if(!($i++ % 3)) {
  186. echo '<br>';
  187. }
  188. if (++$i == 1) break;
  189. }
  190. }
  191. }
  192. function lists() {
  193. require_once('lib/html_dom.php');
  194. $url = 'https://davidawindham.com/til/lists/';
  195. $html = file_get_html($url);
  196. $i = 0;
  197. foreach ($html->find('.markdown ul li') as $li) {
  198. if (!empty($html)) {
  199. echo $li;
  200. if (++$i == 7) break;
  201. }
  202. }
  203. }
  204. function lists_code() {
  205. require_once('lib/html_dom.php');
  206. $url = 'https://code.davidawindham.com/david/til/src/main/lists';
  207. $html = file_get_html($url);
  208. $i = 0;
  209. foreach ($html->find('table tr td') as $li) {
  210. if (!empty($html)) {
  211. //if($i++ == 1) continue;
  212. if (++$i == 2) break;
  213. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  214. if(!($i++ % 3)) {
  215. echo '<br>';
  216. }
  217. }
  218. }
  219. }
  220. function notes() {
  221. require_once('lib/html_dom.php');
  222. $url = 'https://davidawindham.com/til/notes/';
  223. $html = file_get_html($url);
  224. $i = 0;
  225. foreach ($html->find('.markdown ul li') as $li) {
  226. if (!empty($html)) {
  227. echo $li;
  228. if (++$i == 7) break;
  229. }
  230. }
  231. }
  232. function listening() {
  233. require_once('lib/html_dom.php');
  234. $url = 'https://davidawindham.com/studio/music/';
  235. $html = file_get_html($url);
  236. $i = 0;
  237. foreach ($html->find('ul .wtracks li') as $li) {
  238. if (!empty($html)) {
  239. echo $li;
  240. if (++$i == 7) break;
  241. }
  242. }
  243. }
  244. function reading() {
  245. require_once('lib/html_dom.php');
  246. $url = 'https://davidawindham.com/til/lists/now/reading';
  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 playing() {
  257. require_once('lib/html_dom.php');
  258. $url = 'https://davidawindham.com/til/lists/now/playing';
  259. $html = file_get_html($url);
  260. $i = 0;
  261. foreach ($html->find('.markdown ul li') as $li) {
  262. if (!empty($html)) {
  263. echo $li;
  264. if (++$i == 7) break;
  265. }
  266. }
  267. }
  268. function watching() {
  269. require_once('lib/html_dom.php');
  270. $url = 'https://davidawindham.com/til/lists/now/watching';
  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. }