utils.php 10 KB

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