| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WP-ShowHide |
| 4 |
Plugin URI: https://lesterchan.net/portfolio/programming/php/ |
| 5 |
Description: Allows you to embed content within your blog post via WordPress ShortCode API and toggling the visibility of the content via a link. By default the content is hidden and user will have to click on the "Show Content" link to toggle it. Similar to what Engadget is doing for their press releases. Example usage: <code>[showhide type="pressrelease"]Press Release goes in here.[/showhide]</code> |
| 6 |
Version: 1.06 |
| 7 |
Author: Lester 'GaMerZ' Chan |
| 8 |
Author URI: https://lesterchan.net |
| 9 |
Text Domain: wp-showhide |
| 10 |
Domain Path: /languages/ |
| 11 |
License: GPL2 |
| 12 |
*/ |
| 13 |
|
| 14 |
/* Copyright 2025 Lester Chan (email : lesterchan@gmail.com) |
| 15 |
|
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License, version 2, as |
| 18 |
published by the Free Software Foundation. |
| 19 |
|
| 20 |
This program is distributed in the hope that it will be useful, |
| 21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
GNU General Public License for more details. |
| 24 |
|
| 25 |
You should have received a copy of the GNU General Public License |
| 26 |
along with this program; if not, write to the Free Software |
| 27 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 |
*/ |
| 29 |
|
| 30 |
|
| 31 |
### Function: Enqueue JavaScripts |
| 32 |
add_action( 'wp_enqueue_scripts', 'showhide_scripts' ); |
| 33 |
function showhide_scripts() { |
| 34 |
wp_enqueue_script( 'jquery' ); |
| 35 |
} |
| 36 |
|
| 37 |
### Function: Load Translation |
| 38 |
add_action( 'plugins_loaded', 'showhide_textdomain' ); |
| 39 |
function showhide_textdomain() { |
| 40 |
load_plugin_textdomain( 'wp-showhide', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 41 |
} |
| 42 |
|
| 43 |
### Function: Short Code For Inserting Press Release Into Post |
| 44 |
add_shortcode( 'showhide', 'showhide_shortcode' ); |
| 45 |
function showhide_shortcode( $atts, $content = null ) { |
| 46 |
// Variables |
| 47 |
$post_id = get_the_id(); |
| 48 |
$word_count = number_format_i18n( sizeof( explode( ' ', strip_tags( $content ) ) ) ); |
| 49 |
|
| 50 |
// Extract ShortCode Attributes |
| 51 |
$attributes = shortcode_atts( array( |
| 52 |
'type' => 'pressrelease', |
| 53 |
'more_text' => __( 'Show Press Release (%s More Words)', 'wp-showhide' ), |
| 54 |
'less_text' => __( 'Hide Press Release (%s Less Words)', 'wp-showhide' ), |
| 55 |
'hidden' => 'yes' |
| 56 |
), $atts ); |
| 57 |
|
| 58 |
// More/Less Text |
| 59 |
$more_text = sprintf( $attributes['more_text'], $word_count ); |
| 60 |
$less_text = sprintf( $attributes['less_text'], $word_count ); |
| 61 |
|
| 62 |
// Determine Whether To Show Or Hide Press Release |
| 63 |
$hidden_class = 'sh-hide'; |
| 64 |
$hidden_css = 'display: none;'; |
| 65 |
$hidden_aria_expanded = 'false'; |
| 66 |
if( $attributes['hidden'] === 'no' ) { |
| 67 |
$hidden_class = 'sh-show'; |
| 68 |
$hidden_css = 'display: block;'; |
| 69 |
$hidden_aria_expanded = 'true'; |
| 70 |
$tmp_text = $more_text; |
| 71 |
$more_text = $less_text; |
| 72 |
$less_text = $tmp_text; |
| 73 |
} |
| 74 |
|
| 75 |
// Format HTML Output |
| 76 |
$output = '<div id="' . esc_attr( $attributes['type'] ) . '-link-' . $post_id . '" class="sh-link ' . esc_attr( $attributes['type'] ) . '-link ' . $hidden_class .'"><a href="#" onclick="showhide_toggle(\'' . esc_js( $attributes['type'] ) . '\', ' . $post_id . ', \'' . esc_js( $more_text ) . '\', \'' . esc_js( $less_text ) . '\'); return false;" aria-expanded="' . $hidden_aria_expanded .'"><span id="' . esc_attr( $attributes['type'] ) . '-toggle-' . $post_id . '">' . esc_html( $more_text ). '</span></a></div>'; |
| 77 |
$output .= '<div id="' . esc_attr( $attributes['type'] ) . '-content-' . $post_id . '" class="sh-content ' . esc_attr( $attributes['type'] ) . '-content ' . $hidden_class . '" style="' . $hidden_css . '">' . do_shortcode( $content ) . '</div>'; |
| 78 |
|
| 79 |
return $output; |
| 80 |
} |
| 81 |
|
| 82 |
### Function: Add JavaScript To Footer |
| 83 |
add_action( 'wp_footer', 'showhide_footer' ); |
| 84 |
function showhide_footer() { |
| 85 |
?> |
| 86 |
<?php if( WP_DEBUG ): ?> |
| 87 |
<script type="text/javascript"> |
| 88 |
function showhide_toggle(type, post_id, more_text, less_text) { |
| 89 |
var $link = jQuery("#"+ type + "-link-" + post_id) |
| 90 |
, $link_a = jQuery('a', $link) |
| 91 |
, $content = jQuery("#"+ type + "-content-" + post_id) |
| 92 |
, $toggle = jQuery("#"+ type + "-toggle-" + post_id) |
| 93 |
, show_hide_class = 'sh-show sh-hide'; |
| 94 |
$link.toggleClass(show_hide_class); |
| 95 |
$content.toggleClass(show_hide_class).toggle(); |
| 96 |
if($link_a.attr('aria-expanded') === 'true') { |
| 97 |
$link_a.attr('aria-expanded', 'false'); |
| 98 |
} else { |
| 99 |
$link_a.attr('aria-expanded', 'true'); |
| 100 |
} |
| 101 |
if($toggle.text() === more_text) { |
| 102 |
$toggle.text(less_text); |
| 103 |
$link.trigger( "sh-link:more" ); |
| 104 |
} else { |
| 105 |
$toggle.text(more_text); |
| 106 |
$link.trigger( "sh-link:less" ); |
| 107 |
} |
| 108 |
$link.trigger( "sh-link:toggle" ); |
| 109 |
} |
| 110 |
</script> |
| 111 |
<?php else : ?> |
| 112 |
<script type="text/javascript">function showhide_toggle(e,t,r,g){var a=jQuery("#"+e+"-link-"+t),s=jQuery("a",a),i=jQuery("#"+e+"-content-"+t),l=jQuery("#"+e+"-toggle-"+t);a.toggleClass("sh-show sh-hide"),i.toggleClass("sh-show sh-hide").toggle(),"true"===s.attr("aria-expanded")?s.attr("aria-expanded","false"):s.attr("aria-expanded","true"),l.text()===r?(l.text(g),a.trigger("sh-link:more")):(l.text(r),a.trigger("sh-link:less")),a.trigger("sh-link:toggle")}</script> |
| 113 |
<?php endif; ?> |
| 114 |
<?php |
| 115 |
} |
| 116 |
|