utils.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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://davidawindham.com/bookmarks/';
  114. $html = file_get_html($url);
  115. $i = 0;
  116. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  117. if (!empty($html)) {
  118. echo $li . '<br />';
  119. if (++$i == 9) break;
  120. }
  121. }
  122. }
  123. function bookmarks_pull($limit = 27) {
  124. require_once('lib/html_dom.php');
  125. $url = 'https://davidawindham.com/bookmarks/';
  126. $html = file_get_html($url);
  127. $i = 0;
  128. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  129. if (!empty($html)) {
  130. echo $li . '<br />';
  131. if (++$i == $limit) break;
  132. }
  133. }
  134. }
  135. function bookmarks_all() {
  136. require_once('lib/html_dom.php');
  137. $url = 'https://davidawindham.com/bookmarks/';
  138. $html = file_get_html($url);
  139. $i = 0;
  140. foreach ($html->find('.linklist-item-title h2 a') as $li) {
  141. if (!empty($html)) {
  142. echo $li . '<br />';
  143. }
  144. }
  145. }
  146. /*============================================
  147. Desk block-migration shortcodes
  148. Wrap the echoing helpers (and the inline "Recently Edited" loop from
  149. page-desk.php) so they can be dropped into block content.
  150. ==============================================*/
  151. function dw_til_shortcode() {
  152. ob_start();
  153. pull_til();
  154. return ob_get_clean();
  155. }
  156. add_shortcode( 'dw_til', 'dw_til_shortcode' );
  157. function dw_bookmarks_shortcode() {
  158. ob_start();
  159. bookmarks_pull();
  160. return ob_get_clean();
  161. }
  162. add_shortcode( 'dw_bookmarks', 'dw_bookmarks_shortcode' );
  163. function dw_recently_edited_shortcode() {
  164. $sticky = get_option( 'sticky_posts' );
  165. if ( empty( $sticky ) ) {
  166. return '';
  167. }
  168. $posts = get_posts( array(
  169. 'post_type' => 'post',
  170. 'orderby' => 'modified',
  171. 'posts_per_page' => 5,
  172. 'post__in' => $sticky,
  173. ) );
  174. if ( ! $posts ) {
  175. return '';
  176. }
  177. $out = '<ul>';
  178. foreach ( $posts as $p ) {
  179. $out .= '<li style="color:#777">'
  180. . get_the_modified_date( 'y/m/d', $p ) . ' ( ' . get_the_date( 'y/m/d', $p ) . ' ) - '
  181. . '<a href="' . esc_url( get_permalink( $p ) ) . '">' . esc_html( get_the_title( $p ) ) . '</a></li>';
  182. }
  183. $out .= '</ul>';
  184. return $out;
  185. }
  186. add_shortcode( 'dw_recently_edited', 'dw_recently_edited_shortcode' );
  187. function now() {
  188. require_once('lib/html_dom.php');
  189. $url = 'https://davidawindham.com/til/lists/now';
  190. $html = file_get_html($url);
  191. $i = 0;
  192. foreach ($html->find('.markdown ul li') as $li) {
  193. if (!empty($html)) {
  194. echo $li;
  195. if (++$i == 6) break;
  196. }
  197. }
  198. }
  199. function todo() {
  200. require_once('lib/html_dom.php');
  201. $url = 'https://davidawindham.com/til/lists/todo';
  202. $html = file_get_html($url);
  203. $i = 0;
  204. foreach ($html->find('.markdown ul li') as $li) {
  205. if (!empty($html)) {
  206. echo $li;
  207. if (++$i == 3) break;
  208. }
  209. }
  210. }
  211. function todone() {
  212. require_once('lib/html_dom.php');
  213. $url = 'https://davidawindham.com/til/lists/todone';
  214. $html = file_get_html($url);
  215. $i = 0;
  216. foreach ($html->find('.markdown ul li') as $li) {
  217. if (!empty($html)) {
  218. echo $li;
  219. if (++$i == 3) break;
  220. }
  221. }
  222. }
  223. function docs() {
  224. require_once('lib/html_dom.php');
  225. $url = 'https://davidawindham.com/til/docs';
  226. $html = file_get_html($url);
  227. $i = 0;
  228. foreach ($html->find('.markdown ul li') as $li) {
  229. if (!empty($html)) {
  230. echo $li;
  231. if (++$i == 5) break;
  232. }
  233. }
  234. }
  235. function lists() {
  236. require_once('lib/html_dom.php');
  237. $url = 'https://davidawindham.com/til/lists/';
  238. $html = file_get_html($url);
  239. $i = 0;
  240. foreach ($html->find('.markdown ul li') as $li) {
  241. if (!empty($html)) {
  242. echo $li;
  243. if (++$i == 5) break;
  244. }
  245. }
  246. }
  247. function notes() {
  248. require_once('lib/html_dom.php');
  249. $url = 'https://davidawindham.com/til/notes/';
  250. $html = file_get_html($url);
  251. $i = 0;
  252. foreach ($html->find('.markdown ul li') as $li) {
  253. if (!empty($html)) {
  254. echo $li;
  255. if (++$i == 5) break;
  256. }
  257. }
  258. }
  259. function playing() {
  260. require_once('lib/html_dom.php');
  261. $url = 'https://davidawindham.com/til/lists/now/playing';
  262. $html = file_get_html($url);
  263. $i = 0;
  264. foreach ($html->find('.markdown ul li') as $li) {
  265. if (!empty($html)) {
  266. echo $li;
  267. if (++$i == 3) break;
  268. }
  269. }
  270. }
  271. function watching() {
  272. require_once('lib/html_dom.php');
  273. $url = 'https://davidawindham.com/til/lists/now/watching';
  274. $html = file_get_html($url);
  275. $i = 0;
  276. foreach ($html->find('.markdown ul li') as $li) {
  277. if (!empty($html)) {
  278. echo $li;
  279. if (++$i == 3) break;
  280. }
  281. }
  282. }
  283. function listening() {
  284. require_once('lib/html_dom.php');
  285. $url = 'https://davidawindham.com/til/lists/now/listening';
  286. $html = file_get_html($url);
  287. $i = 0;
  288. foreach ($html->find('.markdown ul li') as $li) {
  289. if (!empty($html)) {
  290. echo $li;
  291. if (++$i == 3) break;
  292. }
  293. }
  294. }
  295. function reading() {
  296. require_once('lib/html_dom.php');
  297. $url = 'https://davidawindham.com/til/lists/now/reading';
  298. $html = file_get_html($url);
  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 learning() {
  308. require_once('lib/html_dom.php');
  309. $url = 'https://davidawindham.com/til/lists/now/learning';
  310. $html = file_get_html($url);
  311. $i = 0;
  312. foreach ($html->find('.markdown ul li') as $li) {
  313. if (!empty($html)) {
  314. echo $li;
  315. if (++$i == 3) break;
  316. }
  317. }
  318. }
  319. /********************************/
  320. /************ Code ************/
  321. /********************************/
  322. function code() {
  323. require_once('lib/html_dom.php');
  324. $url = 'https://code.davidawindham.com/david?tab=activity';
  325. $html = file_get_html($url);
  326. $i = 0;
  327. foreach ($html->find('.content ul li') as $li) {
  328. foreach ($li->find('span.text') as $comment) {
  329. if (!empty($html)) {
  330. $str = array('href="/', 'light');
  331. $replace = array('href="https://code.davidawindham.com/', '');
  332. //echo str_replace($str,$replace, $li . ' - ' . $commit . ' - ' . $comment . '<br>');
  333. echo str_replace($str,$replace, $li . '' . $comment . '<br>');
  334. }
  335. }
  336. //foreach ($html->find('span.text') as $comment) {}
  337. if (++$i == 5) break;
  338. }
  339. }
  340. function commits() {
  341. require_once('lib/html_dom.php');
  342. $url = 'https://code.davidawindham.com/david?tab=activity';
  343. $html = file_get_html($url);
  344. $i = 0;
  345. foreach ($html->find('ul li a') as $li) {
  346. if (!empty($html)) {
  347. $str = array('href="/', 'light');
  348. $replace = array('href="https://code.davidawindham.com/', '');
  349. echo str_replace($str,$replace, date("y/m/d") . ' - ' . $li . '<br>');
  350. if (++$i == 3) break;
  351. }
  352. }
  353. }
  354. function docs_code() {
  355. require_once('lib/html_dom.php');
  356. $url = 'https://code.davidawindham.com/david/til/src/main/docs';
  357. $html = file_get_html($url);
  358. $i = 0;
  359. foreach ($html->find('table tr td') as $li) {
  360. if (!empty($html)) {
  361. //if($i++ == 1) continue;
  362. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  363. if(!($i++ % 3)) {
  364. echo '<br>';
  365. }
  366. if (++$i == 1) break;
  367. }
  368. }
  369. }
  370. function lists_code() {
  371. require_once('lib/html_dom.php');
  372. $url = 'https://code.davidawindham.com/david/til/src/main/lists';
  373. $html = file_get_html($url);
  374. $i = 0;
  375. foreach ($html->find('table tr td') as $li) {
  376. if (!empty($html)) {
  377. //if($i++ == 1) continue;
  378. if (++$i == 2) break;
  379. echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
  380. if(!($i++ % 3)) {
  381. echo '<br>';
  382. }
  383. }
  384. }
  385. }
  386. function photo() {
  387. require_once('lib/html_dom.php');
  388. $url = 'https://photo.davidwindham.com/#16734437965516';
  389. $html = file_get_html($url);
  390. $i = 0;
  391. foreach ($html->find('.photo .overlay h1') as $li) {
  392. if (!empty($html)) {
  393. echo str_replace('href="/', 'href="https://photo.davidawindham.com/', $li . '&nbsp;');
  394. if (++$i == 18) break;
  395. }
  396. }
  397. }
  398. //function mastodon() {
  399. // require_once('lib/html_dom.php');
  400. //
  401. // $url = 'https://universeodon.com/@windhamdavid/with_replies';
  402. // $html = file_get_html($url);
  403. // $i = 0;
  404. // foreach ($html->find('.status__content__text p') as $li) {
  405. // if (!empty($html)) {
  406. // echo str_replace('href="/', 'href="https://universeodon.com/', $li . '&nbsp;');
  407. // if (++$i == 18) break;
  408. // }
  409. // }
  410. //}