| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /**
- * Theme Functions - functions.php
- *
- * 1. Scripts needed to run the vue theme
- */
-
- // Load WP stuff as needed
- // https://www.smashingmagazine.com/2018/10/headless-wordpress-decoupled/#improving-performance-decoupled-json-approach
- define( 'SHORTINIT', true );
- $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
- // require_once filter_var( $parse_uri[0] . 'wp-load.php', FILTER_SANITIZE_STRING );
-
- $path = $_SERVER['DOCUMENT_ROOT'];
-
- require_once $path . '/wp-load.php';
- require_once $path . '/wp-config.php';
- require_once $path . '/wp-includes/wp-db.php';
- require_once $path . '/wp-includes/pluggable.php';
-
- // https://github.com/EvanAgee/vuejs-wordpress-theme-starter/blob/master/functions.php
- remove_action( 'template_redirect', 'redirect_canonical' );
- function remove_redirects() {
- add_rewrite_rule( '^/(.+)/?', 'index.php', 'top' );
- }
- add_action( 'init', 'remove_redirects' );
-
-
- function vue_theme_routes() {
- $routes = array();
-
- $query = new WP_Query( array(
- 'post_type' => 'any',
- 'post_status' => 'publish',
- 'posts_per_page' => -1
- ) );
- if( $query->have_posts() ) {
- while( $query->have_posts() ) {
- $query->the_post();
- $routes[] = array(
- 'id' => get_the_ID(),
- 'type' => get_post_type(),
- 'slug' => basename( get_permalink() ),
- );
- }
- }
- wp_reset_postdata();
-
- return $routes;
- }
-
- /* 1 */
- function vue_theme_scripts() {
-
- wp_enqueue_style( 'style', get_stylesheet_uri() );
-
- if ( defined( 'IS_DEV' ) && IS_DEV ) {
- wp_register_script(
- 'vue-theme',
- 'http://localhost:8081/build/main.js',
- array( 'jquery' ),
- false,
- true
- );
- } else {
- wp_register_script(
- 'vue-theme',
- get_template_directory_uri() . '/public/main.js',
- array( 'jquery' ),
- false,
- true
- );
- }
-
- wp_localize_script( 'vue-theme', 'wp', array(
- 'template' => get_stylesheet_directory_uri(),
- 'rest' => esc_url_raw( rest_url() ),
- 'site_name' => get_bloginfo( 'name' ),
- // 'nonce' => wp_create_nonce( 'wp_rest' ),
- 'routes' => vue_theme_routes(),
- ) );
-
- wp_enqueue_script( 'vue-theme' );
- }
- add_action( 'wp_enqueue_scripts', 'vue_theme_scripts' );
-
- /**
- * disable endpoints in rest api
- **/
- add_filter( 'rest_endpoints', function ( $endpoints ) {
- if ( isset( $endpoints['/wp/v2/users'] ) ) {
- unset( $endpoints['/wp/v2/users'] );
- }
- if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
- unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
- }
-
- if ( isset( $endpoints['/wp/v2/comments'] ) ) {
- unset( $endpoints['/wp/v2/comments'] );
- }
- if ( isset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] ) ) {
- unset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] );
- }
-
- if ( isset( $endpoints['/wp/v2/settings'] ) ) {
- unset( $endpoints['/wp/v2/settings'] );
- }
- return $endpoints;
- } );
-
- /**
- * remove wordpress version number from files
- **/
- remove_action( 'wp_head', 'wp_generator' );
-
- // Disable XML-RPC
- add_filter('xmlrpc_enabled', '__return_false');
- // Disable WLManifest
- remove_action( 'wp_head', 'wlwmanifest_link' ) ;
- // Disable self ping
- function disable_pingback( &$links ) {
- foreach ( $links as $l => $link )
- if ( 0 === strpos( $link, get_option( 'home' ) ) )
- unset($links[$l]);
- }
- add_action( 'pre_ping', 'disable_pingback' );
- // Disable RSD (used with self ping and XML-RPC)
- remove_action( 'wp_head', 'rsd_link' ) ;
- // Disable post embed
- function disable_embed() { wp_dequeue_script( 'wp-embed' ); }
- add_action( 'wp_footer', 'disable_embed' );
- // Disable Shortlinking
- remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
-
- // Remove versions from static assets (prevents cache-ing)
- function remove_cssjs_ver( $src ) {
- if( strpos( $src, '?ver=' ) )
- $src = remove_query_arg( 'ver', $src );
- return $src;
- }
- add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
- add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
-
- // Do NOT include jquery
- function no_default_jquery( ) {
- if (!is_admin()) {
- wp_deregister_script('jquery');
- wp_register_script('jquery', false);
- }
- }
- add_action( 'init', 'no_default_jquery' );
-
- // header( 'Access-Control-Allow-Origin: http://localhost:8080' );
- header( 'Content-Type: application/json' );
|