utils.php 10 KB

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