Plugin Directory

Changeset 3134445


Ignore:
Timestamp:
08/12/2024 06:10:27 PM (20 months ago)
Author:
petro64
Message:

Version 1.4: plugin fixes, mabe by user suggestions:

  • New option to ON/OFF displayed post date
  • New option to ON/OFF displayed post author name
  • Plugin will work without OpenGraph plugins installed (provide proper meta to TG bot) and reuse existing, if your blog sets custom post share metadata
Location:
tg-instantview/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • tg-instantview/trunk/readme.txt

    r3069148 r3134445  
    33Tags: telegram
    44Requires at least: 5.0
    5 Tested up to: 6.4
    6 Stable tag: 1.3
     5Tested up to: 6.6
     6Stable tag: 1.4
    77Requires PHP: 7.0
    88License: GPLv3
     
    5656== Changelog ==
    5757
     58= 1.4 =
     59* Plugin now provides full set of OpenGraph meta information for post and does not require SEO plugins anymore
     60* Add option to show/hide post published date
     61* Add option to show/hide post author
     62
    5863= 1.3 =
    5964* Fixes from Wordpress review: template html, prefixes, license
  • tg-instantview/trunk/tg-admin.php

    r3069148 r3134445  
    4242    {
    4343        // Set class property
    44         $this->options = get_option( 'tgiv_instantview_render' );
     44        $this->options = tgiv_options();
    4545
    4646        ?>
     
    8484            'tgiv_render_options' // Section
    8585        );
     86        add_settings_field(
     87            'tgiv_display_date', // ID
     88            'Display post date?', // Title
     89            array( $this, 'display_date_callback' ), // Callback
     90            'tgiv-instantview-setting-admin', // Page
     91            'tgiv_render_options' // Section
     92        );
     93        add_settings_field(
     94            'tgiv_display_author', // ID
     95            'Display post author?', // Title
     96            array( $this, 'display_author_callback' ), // Callback
     97            'tgiv-instantview-setting-admin', // Page
     98            'tgiv_render_options' // Section
     99        );
    86100    }
    87101
     
    94108    {
    95109        $new_input = array();
    96         if( isset( $input['tgiv_channel_name'] ) ) {
     110        if (isset($input['tgiv_channel_name'])) {
    97111            $channel_name = $input['tgiv_channel_name'];
    98112            $channel_name = trim($channel_name);
     
    105119            $new_input['tgiv_channel_name'] = $channel_name;
    106120        }
     121        $new_input['tgiv_display_date'] = isset($input['tgiv_display_date']);
     122        $new_input['tgiv_display_author'] = isset($input['tgiv_display_author']);
    107123        return $new_input;
    108124    }
     
    126142        );
    127143    }
     144    public function display_date_callback()
     145    {
     146        printf(
     147            '<input type="checkbox" id="tgiv_display_date" name="tgiv_instantview_render[tgiv_display_date]" value="ON" %s/>',
     148            $this->options['tgiv_display_date'] ? 'checked="checked"' : ''
     149        );
     150    }
     151    public function display_author_callback()
     152    {
     153        printf(
     154            '<input type="checkbox" id="tgiv_display_author" name="tgiv_instantview_render[tgiv_display_author]" value="ON" %s/>',
     155            $this->options['tgiv_display_author'] ? 'checked="checked"' : ''
     156        );
     157    }
    128158}
    129159
  • tg-instantview/trunk/tg-display.php

    r3069148 r3134445  
    88    exit;
    99}
    10 $tg_options = get_option( 'tgiv_instantview_render' );
     10// Get Plugin settings array
     11$tgiv_options = tgiv_options();
     12// Check for default values
     13
     14// Get default WP meta tags, going to be displayed for this post
     15$tgiv_wp_meta = tgiv_extract_meta();
     16
     17// Prepare list of meta tags, rendered for preview
     18$tgiv_meta = array();
     19// Required to render:
     20$tgiv_meta['tg:site_verification'] = 'g7j8/rPFXfhyrq5q0QQV7EsYWv4=';
     21// Channel name?
     22if ($tgiv_options['tgiv_channel_name']) {
     23    $tgiv_meta['telegram:channel'] = $tgiv_options['tgiv_channel_name'];
     24}
     25
     26// Published date: article:published_time
     27if ($tgiv_options['tgiv_display_date']) {
     28    $tgiv_meta['article:published_time'] = get_the_date('c');
     29} else {
     30    // Date disabled, display none
     31    $tgiv_meta['article:published_time'] = '';
     32}
     33// Author: article:author
     34if ($tgiv_options['tgiv_display_author']) {
     35    $tgiv_meta['article:author'] = get_the_author();
     36} else {
     37    // Author disabled, display none
     38    $tgiv_meta['article:author'] = '';
     39}
     40// Image: og:image
     41if (isset($tgiv_wp_meta['og:image'])) {
     42    $tgiv_meta['og:image'] = $tgiv_wp_meta['og:image'];
     43} else {
     44    // If not set, get from WP
     45    $tgiv_meta['og:image'] = get_the_post_thumbnail_url();
     46}
     47// Site name: og:site_name
     48$tgiv_meta['og:site_name'] = get_bloginfo('name');
     49// Title: og:title
     50$tgiv_meta['og:title'] = get_the_title();
     51// Short text: og:description
     52if (isset($tgiv_wp_meta['og:description'])) {
     53    $tgiv_meta['og:description'] = $tgiv_wp_meta['og:description'];
     54} else {
     55    // If not set, fill with excert
     56    $tgiv_meta['og:description'] = wp_strip_all_tags(get_the_excerpt());
     57}
     58
    1159?>
    1260<!DOCTYPE html>
    1361<html <?php language_attributes(); ?>>
    1462<head>
    15     <meta charset="<?php bloginfo('charset'); ?>">
    16     <?php if (isset($tg_options['tgiv_channel_name']) && strlen($tg_options['tgiv_channel_name'])) { ?>
    17     <meta property="telegram:channel" content="<?php echo esc_html($tg_options['tgiv_channel_name']); ?>">
     63    <meta charset="<?php bloginfo('charset'); ?>" />
     64    <?php foreach ($tgiv_meta as $tgiv_meta_key => $tgiv_meta_value) { ?>
     65    <meta property="<?php echo esc_html($tgiv_meta_key); ?>" content="<?php echo esc_html($tgiv_meta_value); ?>" />
    1866    <?php } ?>
    19     <meta property="tg:site_verification" content="g7j8/rPFXfhyrq5q0QQV7EsYWv4=">
    20     <?php wp_head(); ?>
    2167</head>
    2268
  • tg-instantview/trunk/tg-instantview.php

    r3069148 r3134445  
    44Plugin URI: https://github.com/petrows/wp-tg-instantview
    55Description: Triggers Telegram InstantView for posts
    6 Version: 1.3
     6Version: 1.4
    77Author: Petro
    88Author URI: https://petro.ws/
     
    1515}
    1616
    17 add_option('tgiv_instanview_channel_name', '');
     17// Define options and defaults:
     18// Displayed Telegram channel name
     19add_option('tgiv_instantview_render', array());
    1820
    1921// Load admin settings
     
    3133
    3234/*
     35Function to get and prepare default options for plugin
     36*/
     37function tgiv_options() {
     38    $options = get_option( 'tgiv_instantview_render' );
     39    if (!isset($options['tgiv_channel_name'])) {
     40        $options['tgiv_channel_name'] = '';
     41    }
     42    if (!isset($options['tgiv_display_date'])) {
     43        $options['tgiv_display_date'] = true;
     44    } else {
     45        $options['tgiv_display_date'] = boolval($options['tgiv_display_date']);
     46    }
     47    if (!isset($options['tgiv_display_author'])) {
     48        $options['tgiv_display_author'] = true;
     49    } else {
     50        $options['tgiv_display_author'] = boolval($options['tgiv_display_author']);
     51    }
     52    return $options;
     53}
     54
     55/*
     56Function to get prepared meta HTML tags from "normal" WP output.
     57We have to use ob_* functions to get HTML, as seems to be there is
     58no better way to get rendered tags from SEO plugins and etc.
     59*/
     60function tgiv_extract_meta() {
     61    ob_start();
     62    wp_head();
     63    $html_output = ob_get_contents();
     64    ob_end_clean();
     65    $meta_out = array();
     66    // Find all meta tags
     67    if (preg_match_all('/<meta([^>]+)\/>/Uuims', $html_output, $out)) {
     68        $html = '';
     69        foreach($out[1] as $meta_contents) {
     70            // Extract HTML attributes
     71            $meta_name = '';
     72            $meta_value = '';
     73            if (preg_match('/property="([^"]*)"/Uuims', $meta_contents, $meta_v)) {
     74                $meta_name = $meta_v[1];
     75            } else if (preg_match('/property=\'([^\']*)\'/Uuims', $meta_contents, $meta_v)) {
     76                $meta_name = $meta_v[1];
     77            }
     78            if (preg_match('/content="([^"]*)"/Uuims', $meta_contents, $meta_v)) {
     79                $meta_value = $meta_v[1];
     80            } else if (preg_match('/content=\'([^\']*)\'/Uuims', $meta_contents, $meta_v)) {
     81                $meta_value = $meta_v[1];
     82            }
     83            if ($meta_name) {
     84                $meta_out[$meta_name] = htmlspecialchars_decode($meta_value);
     85            }
     86        }
     87    }
     88    return $meta_out;
     89}
     90
     91/*
    3392Telegram InstantView does not expand built-in Gutenberg gallery,
    3493with nested <figure> with other figures inside. More precise, it
     
    3897display them as a nice built-in images gallery.
    3998*/
    40 function tgiv_extract_gallery($block_content)
    41 {
     99function tgiv_extract_gallery($block_content) {
    42100    // Find all images
    43101    if (preg_match_all('/<img[^>]+\/>/', $block_content, $out)) {
Note: See TracChangeset for help on using the changeset viewer.