Skip to content

Instantly share code, notes, and snippets.

@ideag
Created April 9, 2019 16:58
Show Gist options
  • Save ideag/496b0f6e3f284bfb8b049a146bdd2f5f to your computer and use it in GitHub Desktop.
Save ideag/496b0f6e3f284bfb8b049a146bdd2f5f to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Hello VDU
* Author: Arūnas Liuiza [KAYAK]
* Version: 0.1.0
* Text Domain: hello-vdu
*/
add_action( 'plugins_loaded', 'hello_vdu' );
function hello_vdu() {
if ( false === Hello_VDU_Class::$instance ) {
Hello_VDU_Class::$instance = new Hello_VDU_Class();
}
return Hello_VDU_Class::$instance;
}
class Hello_VDU_Class {
public static $instance = false;
public function __construct() {
add_filter( 'the_content', array( $this, 'copyright' ), 100 );
// add "updated" info to the title
add_filter( 'the_title', array( $this, 'modified' ), 10, 2 );
// add icons to post title
add_filter( 'the_title', array( $this, 'icons' ), 100 );
add_action( 'wp_enqueue_scripts', array( $this, 'assets') );
}
public function copyright( $content ) {
if ( ! is_singular( 'post' ) ) {
return $content;
}
$content .= '<hr />' . PHP_EOL;
$content .= '<p><small>';
$content .= sprintf(
__( 'The conents of this article cannot be republished in any form without a prior written consent of the owners of %s website.', 'hello-vdu' ),
get_bloginfo( 'title' )
);
$content .= '</small></p>';
return $content;
}
public function modified( $title, $post_id ) {
if ( is_admin() ) {
return $title;
}
if ( ! in_the_loop() ) {
return $title;
}
$post = get_post( $post_id );
if ( $post->post_date === $post->post_modified ) {
return $title;
}
if ( 3 * HOUR_IN_SECONDS < current_time('timestamp') - strtotime( $post->post_modified ) ) {
return $title;
}
$diff = sprintf(
__( '(updated %s ago)', 'hello-vdu' ),
human_time_diff( strtotime( $post->post_modified ) )
);
$title .= " <small style='color:red;'>{$diff}</small>";
return $title;
}
public function icons( $title ) {
if ( is_admin() ) {
return $title;
}
$patterns = array(
'[foto]' => '<span class="dashicons dashicons-camera"></span>',
'[video]' => '<span class="dashicons dashicons-video-alt2"></span>',
'[audio]' => '<span class="dashicons dashicons-format-audio"></span>',
);
$title = str_replace(
array_keys( $patterns ),
array_values( $patterns ),
$title
);
return $title;
}
public function assets() {
wp_enqueue_style( 'dashicons' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment