Skip to content

Instantly share code, notes, and snippets.

@todiadiyatmo
Last active February 17, 2019 15:38
Show Gist options
  • Save todiadiyatmo/5a3cc4b6be6944bf3c091182ddd0cc54 to your computer and use it in GitHub Desktop.
Save todiadiyatmo/5a3cc4b6be6944bf3c091182ddd0cc54 to your computer and use it in GitHub Desktop.
Source Code Optimasi PWA
<?php
// remove AMP
// remove AMP for POST
add_action( 'init', function(){
return;
remove_post_type_support( 'post', amp_get_slug() );
}, 100);
// redirect 301
add_action( 'send_headers', function() {
global $wp;
// get post id from request url
$post_id = url_to_postid($wp->request);
// not single page
if( $post_id === 0 ) {
return;
}
$post = get_post($post_id);
// not a post
if( $post === null ) {
return;
}
// post_type post condition
if( $post->post_type == 'post' ) {
//get last slash request url
$amp = explode("/", $wp->request);
$last = (count($amp) - 1);
if( $amp[$last] == 'amp' ) {
wp_redirect( get_the_permalink($post_id), 301 );
die();
}
}
});
<?php
add_filter( 'amp_post_template_file', 'cpt_amp_set_template', 10, 3 );
function cpt_amp_set_template( $file, $type, $post ) {
if ( 'single' === $type && 'publikasi' === $post->post_type ) {
$file = dirname( __FILE__ ) . '/amp/single-publikasi.php'; // cek posisi file, untuk mendapatkan path yang benar
}
return $file;
}
<?php
// Add critical CSS here
function wp_enqueue_critical_css() {
// All critical style
wp_enqueue_style( 'wp_critical_style', get_template_directory_uri() . '/wp_critical_style.css' );
// frontpage only
if( is_frontpage() ) {
wp_enqueue_style( 'wp_critical_style_frontpage', get_template_directory_uri() . '/wp_critical_style_frontpage.css' );
}
}
add_action( 'wp_enqueue_scripts', 'wp_enqueue_critical_css' );
// Add Non Critical CSS here
function wp_enqueue_normal_css() {
?>
<noscript id="deferred-styles">
<?php
wp_enqueue_style( 'wp_normal_style', get_template_directory_uri() . '/wp_normal_style.css' );
?>
<!-- W3TC-include-css -->
</noscript>
<script type="text/javascript">
// CSS async
var loadDeferredStyles = function() {
var addStylesNode = document.getElementById("deferred-styles");
var replacement = document.createElement("div");
replacement.innerHTML = addStylesNode.textContent;
document.head.appendChild(replacement)
addStylesNode.parentElement.removeChild(addStylesNode);
};
var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
if (raf) raf(function() { window.setTimeout(loadDeferredStyles, 0); });
else window.addEventListener('load', loadDeferredStyles);
</script>
});
<?php
}
add_action( 'wp_enqueue_scripts', 'wp_enqueue_normal_css' );
<?php
<?php
/**
* Plugin Name: Critical Inline CSS
* Description: Convert css file into inline style used for optimize pwa
* Author: tonjoo
* Author URI: http://tonjoostudio.com/
* Plugin URI: http://tonjoostudio.com
* Version: 1.0
* Text Domain: critical-inline-css
*
* @package CriticalInlineCss
*/
/**
* class inline critical css dengan 1 kondisi homepage
*/
class CriticalInlineCss {
/**
* variable list of crtical file
*/
public $critical_file = array();
/**
* constructor
*/
public function __construct() {
add_action('wp_head', array( $this, 'wp_display_inline_critical_css') );
}
/**
* display inlince css for css file
*/
public function wp_display_inline_critical_css () {
// css directory dummy
$list_file = [];
$list_file['homepage'] = get_stylesheet_directory().'/dist/css/critical-style.min.css';
$list_file['banner'] = get_stylesheet_directory().'/dist/css/banner.min.css';
// allowed in homepage
$allowed_homepage = array('homepage','banner');
// if empty this list css
if( empty($list_file) )
return;
// jika halaman adalah frontpage atau homepage
if( is_home() || is_front_page() ){
foreach ($list_file as $row_key => $row_file) {
// allowed homepage css
if( in_array($row_key, $allowed_homepage) && file_exists($row_file) ) {
$this->critical_file[$row_key] = $row_file ;
}
}
// call forntend function
$this->wp_frontend_inline_critical_css($this->critical_file);
return;
}
// general page
foreach ($list_file as $row_key => $row_file) {
// allowed general page css except homepage css
if( !in_array($row_key, $allowed_homepage) && file_exists($row_file) ) {
$this->critical_file[$row_key] = $row_file ;
}
}
// call forntend function
$this->wp_frontend_inline_critical_css($this->critical_file);
return;
}
/**
* frontend inline critical css function
*/
public function wp_frontend_inline_critical_css( $critical_file=array() ){
// if not empty and is mobile
if( !empty($critical_file) && wp_is_mobile() ){
echo '<style type="text/css">.halo{border:0;}';
foreach ($critical_file as $row_file ) {
include ( $row_file );
}
echo '</style>';
}
}
}
return new CriticalInlineCss();
<?php
$permalink = get_the_permalink($id) . 'amp/';
$site_url_flush = str_replace('.', '-', home_url());
$remove_http = preg_replace('#^https?://#', '', rtrim($permalink, '/'));
$tonjoo_flush = wp_remote_get($site_url_flush . '.cdn.ampproject.org/update-ping/c/s/' . $remove_http);
$tonjoo_visit = wp_remote_get($site_url_flush . '.cdn.ampproject.org/c/s/' . $remove_http);
if (isset($tonjoo_visit['response'])) {
if ($tonjoo_visit['response']['code'] == 200) {
$succes_msg = $site_url_flush . '.cdn.ampproject.org/update-ping/c/s/' . $remove_http . " has been flushed" . PHP_EOL;
WP_CLI::success($succes_msg);
}
else {
// $error_msg = $site_url_flush.'.cdn.ampproject.org/update-ping/c/s/'.$remove_http." wrong url ". PHP_EOL;
// WP_CLI::error( $error_msg, false );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment