Source for file class.ycwp_qrme.php
Documentation is available at class.ycwp_qrme.php
Description: Generate QR Code images
License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
Author: Nicola Mustone <mail@nicolamustone.it>
Author URI: http://www.nicolamustone.it
Copyright 2012 Nicola Mustone (mail@nicolamustone.it)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
require_once( 'class.qrcode.php' );
include_once( 'qrme.widget.php' );
* This class can handle qr code images through QRCode class (class.qrcode.php).
* It uses Google Charts APIs to generate QR Code image used in blog posts.
* @author Nicola Mustone <mail@nicolamustone.it>
* @copyright Copyright (c) 2012 Nicola Mustone
* Initializes properties, sets filters, actions and shortcodes
//Properties initialization
//Localization initialization
add_action( 'init', array( &$this, 'ycwp_qrme_localize' ) );
add_filter( 'the_content', array( &$this, 'ycwp_qrme_print_image' ) );
add_filter( 'the_permalink', array( &$this, 'ycwp_qrme_retrive_post_permalink' ) );
add_filter( 'the_title', array( &$this, 'ycwp_qrme_retrive_post_title' ) );
register_activation_hook( __FILE__ , array( &$this, 'ycwp_qrme_set_defaults_on_init' ) );
//Initialize option group in backend.
add_action( 'admin_init', array( &$this, 'ycwp_qrme_register_opt_group' ) );
//Add the option page link.
add_action( 'admin_menu', array( &$this, 'ycwp_qrme_option_page' ) );
add_action( 'wp_enqueue_scripts', array( &$this, 'ycwp_qrme_enqueue_stuff' ) );
add_shortcode( 'qrme', array( &$this, 'ycwp_qrme_shortcode' ) );
//Initialize the class QRCode
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-easing', WP_PLUGIN_URL . 'ycwp-qrme/js/jquery-easing1.3.js', array( 'jquery' ), '1.3' );
//Looks first for user's style
$css = get_template_directory_uri() . '/ycwp-qrme.css';
$css = WP_PLUGIN_URL . '/ycwp-qrme/css/ycwp-qrme.css';
wp_enqueue_style( 'ycwp-qrme-style', $css, false, self::VERSION, 'screen' );
add_options_page( 'YCWP QR Me options', 'YCWP QR Me', 'administrator', 'ycwp-qrme-options', array( &$this, 'ycwp_qrme_option_form' ) );
add_filter( 'plugin_action_links', array( &$this, 'ycwp_qrme_settings_link' ), 10, 2 );
* Adds the Settings link to the plugin activation page
$link = '<a href="options-general.php?page=ycwp-qrme-options">' . __( 'Settings', 'ycwp-qrme' ) . '</a>';
* Retrives the permalink of the current post
* Retrives the title of the current post
* Defines the defaults values for the plugin.
add_option( 'ycwp_qrme_size', $this->_qr->_width );
add_option( 'ycwp_qrme_error', $this->_qr->_settings['chld'] );
add_option( 'ycwp_qrme_classes', ' ' );
add_option( 'ycwp_qrme_add_to_the_content', 'true' );
add_option( 'ycwp_qrme_only_posts', 'false' );
add_option( 'ycwp_qrme_hide', 'true' );
add_option( 'ycwp_qrme_effect', 'fade' );
* Registers plugin options
register_setting( 'ycwp_qrme_opt_group', 'ycwp_qrme_size' );
register_setting( 'ycwp_qrme_opt_group', 'ycwp_qrme_error' );
register_setting( 'ycwp_qrme_opt_group', 'ycwp_qrme_classes' );
register_setting( 'ycwp_qrme_opt_group', 'ycwp_qrme_add_to_the_content' );
register_setting( 'ycwp_qrme_opt_group', 'ycwp_qrme_only_posts' );
register_setting( 'ycwp_qrme_opt_group', 'ycwp_qrme_hide' );
register_setting( 'ycwp_qrme_opt_group', 'ycwp_qrme_effect' );
load_plugin_textdomain( 'ycwp-qrme', false, basename( dirname( __FILE__ ) ) . '/i18n' );
if( ( !is_single() AND !is_home() AND !is_category() AND !is_archive() AND !is_tag() ) OR ( is_search() AND is_page() ) ) {
$add_to_content = get_option( 'ycwp_qrme_add_to_the_content' );
$only_posts = get_option( 'ycwp_qrme_only_posts' );
if( $add_to_content === 'true' ) {
if( $only_posts === 'true' AND !is_single() ) {
* Prints a QR Code using a shortcode.
* - [qrme]http://www.example.com[/qrme]
* - [qrme content="http://www.example.com"]
* Examples with all optional attributes:
* - [qrme size="200" error="L" class="my-custom-class" title="My Example website" alt="A QR Code for my link"]http://www.example.com[/qrme]
* - [qrme size="200" error="L" class="my-custom-class" title="My Example website" alt="A QR Code for my link" content="http://www.example.com"]
$attrs = shortcode_atts( array(
'size' => get_option( 'ycwp_qrme_size' ),
'error' => get_option( 'ycwp_qrme_error' ),
$params = preg_replace( '/[^\w\-]/', ' ', $attrs['class'] );
'class' => $params . ' ycwp-qrme-image',
'title' => $attrs['title'],
if( empty( $params['class'] ) ) {
unset ( $params['class'] );
$content = $attrs['content'];
$qr->setSize( $attrs['size'], $attrs['size'] );
$qr->setErrorLevel( $attrs['error'] );
$qr->setContent( $content );
$image = $qr->QR_POST( $params );
$return .= '<div class="ycwp-qrme-box">';
$return .= '<a href="void: javascript" title="Toggle QR Code" alt="Toggle QR Code" class="ycwp-qrme-toggle"></a><br />';
return $content . $return;
* Add a QR Code image to the post content.
$size = get_option( 'ycwp_qrme_size' );
$params = preg_replace( '/[^\w\-,]/', '', get_option( 'ycwp_qrme_classes' ) );
'class' => str_replace( ',', ' ', $params ) . ' ycwp-qrme-image',
$qr->setSize( $size, $size );
$qr->setErrorLevel( get_option( 'ycwp_qrme_error' ) );
$image = $qr->QR_POST( $params );
$return .= '<div class="ycwp-qrme-box">';
$return .= '<a href="void: javascript" title="Toggle QR Code" alt="Toggle QR Code" class="ycwp-qrme-toggle"></a><br />';
return $content . $return;
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
if( !$( '#ycwp_qrme_add_to_the_content' ).is( ':checked' ) ) {
$( '#ycwp_qrme_only_posts' ).attr( 'disabled', 'disabled' );
$( '#ycwp_qrme_hide' ).attr( 'disabled', 'disabled' );
$( '#ycwp_qrme_add_to_the_content' ).click( function() {
if( !$( '#ycwp_qrme_add_to_the_content' ).is( ':checked' ) ) {
$( '#ycwp_qrme_only_posts' ).attr( 'disabled', 'disabled' );
$( '#ycwp_qrme_hide' ).attr( 'disabled', 'disabled' );
$( '#ycwp_qrme_only_posts' ).attr( 'checked', false );
$( '#ycwp_qrme_only_posts' ).removeAttr( 'disabled' );
$( '#ycwp_qrme_hide' ).attr( 'checked', false );
$( '#ycwp_qrme_hide' ).removeAttr( 'disabled' );
<div class="icon32" id="icon-options-general"></div>
<h2> <?php _e( 'YCWP QR Me Configuration', 'ycwp-qrme' ); ?></h2>
<form method="post" action="options.php">
<?php settings_fields( 'ycwp_qrme_opt_group' ); ?>
<table class="form-table">
<label for="ycwp_qrme_size"> <?php _e( 'Image size', 'ycwp-qrme' ); ?>:</label>
<select name="ycwp_qrme_size" id="ycwp_qrme_size">
<option value="100" <?php selected( get_option( 'ycwp_qrme_size'), '100' ); ?>>100x100 px</option>
<option value="150" <?php selected( get_option( 'ycwp_qrme_size'), '150' ); ?>>150x150 px</option>
<option value="200" <?php selected( get_option( 'ycwp_qrme_size'), '200' ); ?>>200x200 px</option>
<option value="250" <?php selected( get_option( 'ycwp_qrme_size'), '250' ); ?>>250x250 px</option>
<option value="300" <?php selected( get_option( 'ycwp_qrme_size'), '300' ); ?>>300x300 px</option>
<span class="description"> <?php _e( 'QR Code image size ( 200x200px raccomanded )', 'ycwp-qrme' ); ?>.</span>
<label for="ycwp_qrme_error"> <?php _e( 'Error correction level', 'ycwp-qrme' ); ?>:</label>
<select name="ycwp_qrme_error" id="ycwp_qrme_error">
<option value="L" <?php selected( get_option( 'ycwp_qrme_error' ), 'L' ); ?>>L</option>
<option value="M" <?php selected( get_option( 'ycwp_qrme_error' ), 'M' ); ?>>M</option>
<option value="Q" <?php selected( get_option( 'ycwp_qrme_error' ), 'Q' ); ?>>Q</option>
<option value="H" <?php selected( get_option( 'ycwp_qrme_error' ), 'H' ); ?>>H</option>
<br /><span class="description"> <?php _e( 'L = 7% of codewords can be restored', 'ycwp-qrme' ); ?>.</span>
<br /><span class="description"> <?php _e( 'M = 15% of codewords can be restored', 'ycwp-qrme' ); ?>.</span>
<br /><span class="description"> <?php _e( 'Q = 25% of codewords can be restored', 'ycwp-qrme' ); ?>.</span>
<br /><span class="description"> <?php _e( 'H = 30% of codewords can be restored', 'ycwp-qrme' ); ?>.</span>
<label for="ycwp_qrme_classes"> <?php _e( 'Additional image classes', 'ycwp-qrme' ); ?>:</label>
<input type="text" name="ycwp_qrme_classes" id="ycwp_qrme_classes" value=" <?php echo get_option( 'ycwp_qrme_classes' ); ?>" />
<span class="description"> <?php _e( 'Comma separated classes and only <strong>alphanumeric chars</strong>, <strong>-</strong> and <strong>_</strong>', 'ycwp-qrme' ); ?></span>
<label for="ycwp_qrme_add_to_the_content"> <?php _e( 'Add QR Code to posts content', 'ycwp-qrme' ); ?>:</label>
<input type="checkbox" name="ycwp_qrme_add_to_the_content" id="ycwp_qrme_add_to_the_content" value="true" <?php checked (get_option( 'ycwp_qrme_add_to_the_content' ), 'true' ); ?> />
<label for="ycwp_qrme_only_posts"> <?php _e( 'Display QR Code only on single posts page', 'ycwp-qrme' ); ?>:</label>
<input type="checkbox" name="ycwp_qrme_only_posts" id="ycwp_qrme_only_posts" value="true" <?php checked (get_option( 'ycwp_qrme_only_posts' ), 'true' ); ?> />
<span class="description"> <?php _e( 'If checked, only shows QR Code on posts page', 'ycwp-qrme' ); ?>.</span>
<label for="ycwp_qrme_hide"> <?php _e( 'Hide on startup', 'ycwp-qrme' ); ?>:</label>
<input type="checkbox" name="ycwp_qrme_hide" id="ycwp_qrme_hide" value="true" <?php checked (get_option( 'ycwp_qrme_hide' ), 'true' ); ?> />
<span class="description"> <?php _e( 'If checked, only display a link to show the QR Code, also for shortcodes', 'ycwp-qrme' ); ?>.</span>
<label for="ycwp_qrme_effect"> <?php _e( 'Show/Hide effect', 'ycwp-qrme' ); ?>:</label>
<select name="ycwp_qrme_effect" id="ycwp_qrme_effect">
<option value="fade" <?php selected( get_option( 'ycwp_qrme_effect' ), 'fade' ); ?>>fade</option>
<option value="slide" <?php selected( get_option( 'ycwp_qrme_effect' ), 'slide' ); ?>>slide</option>
<option value="no_effect" <?php selected( get_option( 'ycwp_qrme_effect' ), 'no_effect' ); ?>>no effect</option>
<span class="description"> <?php _e( 'jQuery effect', 'ycwp-qrme' ); ?>.</span>
<input type="submit" id="submit" name="submit" value=" <?php _e( 'Save', 'ycwp-qrme' ); ?>" />
* Print jQuery code for show/hide effect
switch( get_option( 'ycwp_qrme_effect' ) ) {
case 'fade': $effect = 'fade'; break;
case 'slide': $effect = 'slide'; break;
case 'no_effect': $effect = 'no_effect'; break;
default: $effect = 'fade';
if( $effect == 'fade' ) {
$( ".ycwp-qrme-toggle" ).click( function() {
$(this).parent().find( ".ycwp-qrme-image").fadeToggle( "slow" );
$(this).text( $(this).text() == "' . __( 'Show QR Code', 'ycwp-qrme' ) . '" ? "' . __( 'Hide QR Code', 'ycwp-qrme' ) . '" : "' . __( 'Show QR Code', 'ycwp-qrme' ) . '" );
} else if( $effect == 'slide' ) {
$( ".ycwp-qrme-toggle" ).click( function() {
$(this).parent().find( ".ycwp-qrme-image").slideToggle( "slow" );
$(this).text( $(this).text() == "' . __( 'Show QR Code', 'ycwp-qrme' ) . '" ? "' . __( 'Hide QR Code', 'ycwp-qrme' ) . '" : "' . __( 'Show QR Code', 'ycwp-qrme' ) . '" );
} else if( $effect == 'no_effect' ) {
$( ".ycwp-qrme-toggle" ).click( function() {
$(this).parent().find( ".ycwp-qrme-image").toggle();
$(this).text( $(this).text() == "' . __( 'Show QR Code', 'ycwp-qrme' ) . '" ? "' . __( 'Hide QR Code', 'ycwp-qrme' ) . '" : "' . __( 'Show QR Code', 'ycwp-qrme' ) . '" );
<script type="text/javascript">
var ycwp_qrme_hide = "' . get_option( 'ycwp_qrme_hide' ) . '";
jQuery( document ).ready( function( $ ) {
var hide_on_startup = false;
if( ycwp_qrme_hide == "true" ) {
$( ".ycwp-qrme-image" ).hide();
$( ".ycwp-qrme-toggle" ).text( "' . __( 'Show QR Code', 'ycwp-qrme' ) . '" );
$( ".ycwp-qrme-toggle" ).text( "' . __( 'Hide QR Code', 'ycwp-qrme' ) . '" );
width: ' . get_option( 'ycwp_qrme_size' ) . 'px;
|