<?php

/**
 * Filter to Run WP REST API v2 Endpoints and Fix double Slash in rest_url
 */

//add_filter( 'rest_url_prefix', function () { return 'wp-json/wp/v2'; } );

//add_filter( 'rest_url_prefix', 'fix_json_endpoint' );
//function fix_json_endpoint (){
//	$json_endpoint = 'wp-json/wp/v2';
//	return rtrim( $json_endpoint, '/' );
//}

//remove_filter('json_dispatch_request', 'json_v1_compatible_dispatch', 10);
//remove_filter('json_endpoints', 'json_v1_compatible_routes', 1000);


/**
 * Build path data for current request.
 *
 * @return string|bool
 */
function _s_backbone_get_request_path() {
	global $wp_rewrite;

	if ( $wp_rewrite->using_permalinks() ) {
		global $wp;

		// If called too early, bail
		if ( ! isset( $wp->request ) ) {
			return false;
		}

		// Determine path for paginated version of current request
		if ( false != preg_match( '#' . $wp_rewrite->pagination_base . '/\d+/?$#i', $wp->request ) ) {
			$path = preg_replace( '#' . $wp_rewrite->pagination_base . '/\d+$#i', $wp_rewrite->pagination_base . '/%d', $wp->request );
		} else {
			$path = $wp->request . '/' . $wp_rewrite->pagination_base . '/%d';
		}

		// Slashes everywhere we need them
		if ( 0 !== strpos( $path, '/' ) )
			$path = '/' . $path;

		$path = user_trailingslashit( $path );
	} else {
		// Clean up raw $_REQUEST input
		$path = array_map( 'sanitize_text_field', $_REQUEST );
		$path = array_filter( $path );

		$path['paged'] = '%d';

		$path = add_query_arg( $path, '/' );
	}

	return empty( $path ) ? false : $path;
}

/**
 * Return query string for current request, prefixed with '?'.
 *
 * @return string
 */
function _s_backbone_get_request_parameters() {
	$uri = $_SERVER[ 'REQUEST_URI' ];
	$uri = preg_replace( '/^[^?]*(\?.*$)/', '$1', $uri, 1, $count );
	if ( $count != 1 ) {
		return '';
	}

	return $uri;
}

/*******************
< PHP 7.3 include file as dom.php
>= PHP 7.3 include file as html_dom.php
**********************/

function til() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/index.html';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('ul li ul li') as $li) {
	    echo $li;
		if (++$i == 9) break;
	}
}
function pull_til() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/index.html';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('ul li ul li') as $li) {
	    echo $li;
		if (++$i == 50) break;
	}
}
function pull_til_all() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/index.html';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('ul li ul li') as $li) {
	    echo $li;
	}
}
function bookmarks() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/bookmarks/';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.linklist-item-title h2 a') as $li) {
	    if (!empty($html)) {
			echo $li . '<br />';
			if (++$i == 9) break;
		}
	}
}
function bookmarks_pull() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/bookmarks/';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.linklist-item-title h2 a') as $li) {
	    if (!empty($html)) {
			echo $li . '<br />';
			if (++$i == 27) break;
		}
	}
}
function bookmarks_all() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/bookmarks/';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.linklist-item-title h2 a') as $li) {
	    if (!empty($html)) {
			echo $li . '<br />';
		}
	}
}

function now() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/lists/now';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
    if (!empty($html)) {
      echo $li;
      if (++$i == 4) break;
    }
  }
}
function todo() {
	require_once('lib/html_dom.php');

	$url = 'https://davidawindham.com/til/lists/todo';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
    if (!empty($html)) {
      echo $li;
      if (++$i == 3) break;
    }
  }
}
function todone() {
	require_once('lib/html_dom.php');

	$url = 'https://davidawindham.com/til/lists/todone';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
    if (!empty($html)) {
      echo $li;
      if (++$i == 3) break;
    }
  }
}

function docs() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/docs';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 5) break;
		}
	}
}
function lists() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/lists/';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 5) break;
		}
	}
}
function notes() {
	require_once('lib/html_dom.php');

	$url = 'https://davidawindham.com/til/notes/';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 5) break;
		}
	}
}

function playing() {
	require_once('lib/html_dom.php');

	$url = 'https://davidawindham.com/til/lists/now/playing';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 3) break;
		}
	}
}
function watching() {
	require_once('lib/html_dom.php');

	$url = 'https://davidawindham.com/til/lists/now/watching';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 3) break;
		}
	}
}
function listening() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/lists/now/listening';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 3) break;
		}
	}
}
function reading() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/lists/now/reading';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 3) break;
		}
	}
}
function learning() {
	require_once('lib/html_dom.php');
	$url = 'https://davidawindham.com/til/lists/now/learning';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.markdown ul li') as $li) {
	    if (!empty($html)) {
			echo $li;
      if (++$i == 3) break;
		}
	}
}

/********************************/
/************  Code  ************/
/********************************/

function code() {
	require_once('lib/html_dom.php');

	$url = 'https://code.davidawindham.com/david?tab=activity';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.content ul li') as $li) {
    foreach ($li->find('span.text') as $comment) {

      if (!empty($html)) {
        $str = array('href="/', 'light');
        $replace = array('href="https://code.davidawindham.com/', '');
        //echo str_replace($str,$replace, $li . ' - ' . $commit . ' - ' . $comment . '<br>');
        echo str_replace($str,$replace, $li . '' . $comment . '<br>');
      }
    }
    //foreach ($html->find('span.text') as $comment) {}
    if (++$i == 5) break;

  }
}
function commits() {
	require_once('lib/html_dom.php');

	$url = 'https://code.davidawindham.com/david?tab=activity';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('ul li a') as $li) {
    if (!empty($html)) {
      $str = array('href="/', 'light');
      $replace = array('href="https://code.davidawindham.com/', '');
      echo str_replace($str,$replace, date("y/m/d") . ' - ' . $li . '<br>');
      if (++$i == 3) break;
    }
  }
}
function docs_code() {
	require_once('lib/html_dom.php');

	$url = 'https://code.davidawindham.com/david/til/src/main/docs';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('table tr td') as $li) {
	    if (!empty($html)) {
      //if($i++ == 1) continue;
      echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
      if(!($i++ % 3)) {
        echo '<br>';
      }
      if (++$i == 1) break;
		}
	}
}

function lists_code() {
	require_once('lib/html_dom.php');

	$url = 'https://code.davidawindham.com/david/til/src/main/lists';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('table tr td') as $li) {
	    if (!empty($html)) {
      //if($i++ == 1) continue;
      if (++$i == 2) break;
      echo str_replace('href="/', 'href="https://code.davidawindham.com/', $li);
      if(!($i++ % 3)) {
        echo '<br>';
      }

		}

	}
}

function photo() {
	require_once('lib/html_dom.php');

	$url = 'https://photo.davidwindham.com/#16734437965516';
	$html = file_get_html($url);
	$i = 0;
	foreach ($html->find('.photo .overlay h1') as $li) {
    if (!empty($html)) {
      echo str_replace('href="/', 'href="https://photo.davidawindham.com/', $li . '&nbsp;');
      if (++$i == 18) break;
    }
  }
}


//function mastodon() {
//	require_once('lib/html_dom.php');
//
//	$url = 'https://universeodon.com/@windhamdavid/with_replies';
//	$html = file_get_html($url);
//	$i = 0;
//	foreach ($html->find('.status__content__text p') as $li) {
//    if (!empty($html)) {
//      echo str_replace('href="/', 'href="https://universeodon.com/', $li . '&nbsp;');
//      if (++$i == 18) break;
//    }
//  }
//}