utils.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. // Read the TIL source from the local file when it's on this server (avoids an HTTP
  63. // round-trip per render); fall back to the live URL. Links in the source are
  64. // root-relative or absolute, so they resolve correctly wherever embedded.
  65. function dw_til_html() {
  66. require_once('lib/html_dom.php');
  67. $local = ABSPATH . 'til/index.html';
  68. return file_exists($local) ? file_get_html($local) : file_get_html('https://davidawindham.com/til/index.html');
  69. }
  70. function til($limit = 9) {
  71. $html = dw_til_html();
  72. if (!$html) return;
  73. $i = 0;
  74. foreach ($html->find('ul li ul li') as $li) {
  75. echo $li;
  76. if (++$i == $limit) break;
  77. }
  78. }
  79. function pull_til($limit = 71) { // default matches the Recent Posts column length on page-desk.php
  80. $html = dw_til_html();
  81. if (!$html) return;
  82. $i = 0;
  83. foreach ($html->find('ul li ul li') as $li) {
  84. echo $li;
  85. if (++$i == $limit) break;
  86. }
  87. }
  88. function pull_til_all() {
  89. $html = dw_til_html();
  90. if (!$html) return;
  91. foreach ($html->find('ul li ul li') as $li) {
  92. echo $li;
  93. }
  94. }
  95. // TIL site tag cloud (Docusaurus, /til/posts/tags/) — rendered inline like the WP
  96. // tags so both can share one block. Local file when present; links point to the TIL site.
  97. function dw_til_tags_html() {
  98. require_once('lib/html_dom.php');
  99. $local = ABSPATH . 'til/posts/tags/index.html';
  100. return file_exists($local) ? file_get_html($local) : file_get_html('https://davidawindham.com/til/posts/tags/');
  101. }
  102. function pull_til_tags() {
  103. $html = dw_til_tags_html();
  104. if (!$html) return;
  105. foreach ($html->find('a[rel=tag]') as $a) {
  106. $name = trim(preg_replace('/<span.*$/s', '', $a->innertext)); // drop the trailing count <span>
  107. if ($name === '') continue;
  108. echo '<a href="' . esc_url($a->href) . '">' . esc_html($name) . '</a>, ';
  109. }
  110. }
  111. function bookmarks() {
  112. require_once('lib/html_dom.php');
  113. $url = 'https://davidwindham.com/bookmarks/';
  114. $html = file_get_html($url);
  115. if (!$html) return;
  116. $i = 0;
  117. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  118. if (!empty($html)) {
  119. echo $li . '<br />';
  120. if (++$i == 9) break;
  121. }
  122. }
  123. }
  124. function bookmarks_pull($limit = 27) {
  125. require_once('lib/html_dom.php');
  126. $url = 'https://davidwindham.com/bookmarks/';
  127. $html = file_get_html($url);
  128. if (!$html) return;
  129. $i = 0;
  130. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  131. if (!empty($html)) {
  132. echo $li . '<br />';
  133. if (++$i == $limit) break;
  134. }
  135. }
  136. }
  137. function bookmarks_all() {
  138. require_once('lib/html_dom.php');
  139. $url = 'https://davidwindham.com/bookmarks/';
  140. $html = file_get_html($url);
  141. if (!$html) return;
  142. $i = 0;
  143. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  144. if (!empty($html)) {
  145. echo $li . '<br />';
  146. }
  147. }
  148. }
  149. /*============================================
  150. Desk block-migration shortcodes
  151. Wrap the echoing helpers (and the inline "Recently Edited" loop from
  152. page-desk.php) so they can be dropped into block content.
  153. ==============================================*/
  154. function dw_til_shortcode() {
  155. ob_start();
  156. pull_til();
  157. return ob_get_clean();
  158. }
  159. add_shortcode( 'dw_til', 'dw_til_shortcode' );
  160. function dw_bookmarks_shortcode() {
  161. ob_start();
  162. bookmarks_pull();
  163. return ob_get_clean();
  164. }
  165. add_shortcode( 'dw_bookmarks', 'dw_bookmarks_shortcode' );
  166. function dw_recently_edited_shortcode() {
  167. $sticky = get_option( 'sticky_posts' );
  168. if ( empty( $sticky ) ) {
  169. return '';
  170. }
  171. $posts = get_posts( array(
  172. 'post_type' => 'post',
  173. 'orderby' => 'modified',
  174. 'posts_per_page' => 5,
  175. 'post__in' => $sticky,
  176. ) );
  177. if ( ! $posts ) {
  178. return '';
  179. }
  180. $out = '<ul>';
  181. foreach ( $posts as $p ) {
  182. $out .= '<li style="color:#777">'
  183. . get_the_modified_date( 'y/m/d', $p ) . ' ( ' . get_the_date( 'y/m/d', $p ) . ' ) - '
  184. . '<a href="' . esc_url( get_permalink( $p ) ) . '">' . esc_html( get_the_title( $p ) ) . '</a></li>';
  185. }
  186. $out .= '</ul>';
  187. return $out;
  188. }
  189. add_shortcode( 'dw_recently_edited', 'dw_recently_edited_shortcode' );
  190. function now() {
  191. require_once('lib/html_dom.php');
  192. $url = 'https://davidawindham.com/til/lists/now';
  193. $html = file_get_html($url);
  194. if (!$html) return;
  195. $i = 0;
  196. foreach ($html->find('.markdown ul li') as $li) {
  197. if (!empty($html)) {
  198. echo $li;
  199. if (++$i == 6) break;
  200. }
  201. }
  202. }
  203. function todo() {
  204. require_once('lib/html_dom.php');
  205. $url = 'https://davidawindham.com/til/lists/todo';
  206. $html = file_get_html($url);
  207. if (!$html) return;
  208. $i = 0;
  209. foreach ($html->find('.markdown ul li') as $li) {
  210. if (!empty($html)) {
  211. echo $li;
  212. if (++$i == 3) break;
  213. }
  214. }
  215. }
  216. function todone() {
  217. require_once('lib/html_dom.php');
  218. $url = 'https://davidawindham.com/til/lists/todone';
  219. $html = file_get_html($url);
  220. if (!$html) return;
  221. $i = 0;
  222. foreach ($html->find('.markdown ul li') as $li) {
  223. if (!empty($html)) {
  224. echo $li;
  225. if (++$i == 3) break;
  226. }
  227. }
  228. }
  229. function docs() {
  230. require_once('lib/html_dom.php');
  231. $url = 'https://davidawindham.com/til/docs';
  232. $html = file_get_html($url);
  233. if (!$html) return;
  234. $i = 0;
  235. foreach ($html->find('.markdown ul li') as $li) {
  236. if (!empty($html)) {
  237. echo $li;
  238. if (++$i == 5) break;
  239. }
  240. }
  241. }
  242. function lists() {
  243. require_once('lib/html_dom.php');
  244. $url = 'https://davidawindham.com/til/lists/';
  245. $html = file_get_html($url);
  246. if (!$html) return;
  247. $i = 0;
  248. foreach ($html->find('.markdown ul li') as $li) {
  249. if (!empty($html)) {
  250. echo $li;
  251. if (++$i == 5) break;
  252. }
  253. }
  254. }
  255. function notes() {
  256. require_once('lib/html_dom.php');
  257. $url = 'https://davidawindham.com/til/notes/';
  258. $html = file_get_html($url);
  259. if (!$html) return;
  260. $i = 0;
  261. foreach ($html->find('.markdown ul li') as $li) {
  262. if (!empty($html)) {
  263. echo $li;
  264. if (++$i == 5) break;
  265. }
  266. }
  267. }
  268. function playing() {
  269. require_once('lib/html_dom.php');
  270. $url = 'https://davidawindham.com/til/lists/now/playing';
  271. $html = file_get_html($url);
  272. if (!$html) return;
  273. $i = 0;
  274. foreach ($html->find('.markdown ul li') as $li) {
  275. if (!empty($html)) {
  276. echo $li;
  277. if (++$i == 3) break;
  278. }
  279. }
  280. }
  281. function watching() {
  282. require_once('lib/html_dom.php');
  283. $url = 'https://davidawindham.com/til/lists/now/watching';
  284. $html = file_get_html($url);
  285. if (!$html) return;
  286. $i = 0;
  287. foreach ($html->find('.markdown ul li') as $li) {
  288. if (!empty($html)) {
  289. echo $li;
  290. if (++$i == 3) break;
  291. }
  292. }
  293. }
  294. function listening() {
  295. require_once('lib/html_dom.php');
  296. $url = 'https://davidawindham.com/til/lists/now/listening';
  297. $html = file_get_html($url);
  298. if (!$html) return;
  299. $i = 0;
  300. foreach ($html->find('.markdown ul li') as $li) {
  301. if (!empty($html)) {
  302. echo $li;
  303. if (++$i == 3) break;
  304. }
  305. }
  306. }
  307. function reading() {
  308. require_once('lib/html_dom.php');
  309. $url = 'https://davidawindham.com/til/lists/now/reading';
  310. $html = file_get_html($url);
  311. if (!$html) return;
  312. $i = 0;
  313. foreach ($html->find('.markdown ul li') as $li) {
  314. if (!empty($html)) {
  315. echo $li;
  316. if (++$i == 3) break;
  317. }
  318. }
  319. }
  320. function learning() {
  321. require_once('lib/html_dom.php');
  322. $url = 'https://davidawindham.com/til/lists/now/learning';
  323. $html = file_get_html($url);
  324. if (!$html) return;
  325. $i = 0;
  326. foreach ($html->find('.markdown ul li') as $li) {
  327. if (!empty($html)) {
  328. echo $li;
  329. if (++$i == 3) break;
  330. }
  331. }
  332. }
  333. /********************************/
  334. /************ Code ************/
  335. /********************************/
  336. function code() {
  337. require_once('lib/html_dom.php');
  338. $url = 'https://code.davidawindham.com/david?tab=activity';
  339. $html = file_get_html($url);
  340. if (!$html) return;
  341. $i = 0;
  342. foreach ($html->find('.content ul li') as $li) {
  343. foreach ($li->find('span.text') as $comment) {
  344. if (!empty($html)) {
  345. $str = array('href="/', 'light');
  346. $replace = array('href="https://code.davidawindham.com/', '');
  347. //echo str_replace($str,$replace, $li . ' - ' . $commit . ' - ' . $comment . '<br>');
  348. echo str_replace($str,$replace, $li . '' . $comment . '<br>');
  349. }
  350. }
  351. //foreach ($html->find('span.text') as $comment) {}
  352. if (++$i == 5) break;
  353. }
  354. }
  355. function commits() {
  356. require_once('lib/html_dom.php');
  357. $url = 'https://code.davidawindham.com/david?tab=activity';
  358. $html = file_get_html($url);
  359. if (!$html) return;
  360. $i = 0;
  361. foreach ($html->find('ul li a') as $li) {
  362. if (!empty($html)) {
  363. $str = array('href="/', 'light');
  364. $replace = array('href="https://code.davidawindham.com/', '');
  365. echo str_replace($str,$replace, date("y/m/d") . ' - ' . $li . '<br>');
  366. if (++$i == 3) break;
  367. }
  368. }
  369. }
  370. function docs_code() {
  371. require_once('lib/html_dom.php');
  372. $url = 'https://code.davidawindham.com/david/til/src/main/docs';
  373. $html = file_get_html($url);
  374. if (!$html) return;
  375. $i = 0;
  376. foreach ($html->find('table tr td') as $li) {
  377. if (!empty($html)) {
  378. //if($i++ == 1) continue;
  379. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  380. if(!($i++ % 3)) {
  381. echo '<br>';
  382. }
  383. if (++$i == 1) break;
  384. }
  385. }
  386. }
  387. function lists_code() {
  388. require_once('lib/html_dom.php');
  389. $url = 'https://code.davidawindham.com/david/til/src/main/lists';
  390. $html = file_get_html($url);
  391. if (!$html) return;
  392. $i = 0;
  393. foreach ($html->find('table tr td') as $li) {
  394. if (!empty($html)) {
  395. //if($i++ == 1) continue;
  396. if (++$i == 2) break;
  397. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  398. if(!($i++ % 3)) {
  399. echo '<br>';
  400. }
  401. }
  402. }
  403. }
  404. function photo() {
  405. require_once('lib/html_dom.php');
  406. $url = 'https://photo.davidwindham.com/#16734437965516';
  407. $html = file_get_html($url);
  408. if (!$html) return;
  409. $i = 0;
  410. foreach ($html->find('.photo .overlay h1') as $li) {
  411. if (!empty($html)) {
  412. echo str_replace('href="/', 'href="https://photo.davidawindham.com/', $li . '&nbsp;');
  413. if (++$i == 18) break;
  414. }
  415. }
  416. }
  417. //function mastodon() {
  418. // require_once('lib/html_dom.php');
  419. //
  420. // $url = 'https://universeodon.com/@windhamdavid/with_replies';
  421. // $html = file_get_html($url);
  422. // if (!$html) return;
  423. // $i = 0;
  424. // foreach ($html->find('.status__content__text p') as $li) {
  425. // if (!empty($html)) {
  426. // echo str_replace('href="/', 'href="https://universeodon.com/', $li . '&nbsp;');
  427. // if (++$i == 18) break;
  428. // }
  429. // }
  430. //}