Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements to plugin in course of integration on WordPress VIP #2

Merged
merged 4 commits into from Oct 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions readme.txt
@@ -1,24 +1,30 @@
=== Wedgies Shortcode ===
=== Social Polls by Wedgies.com ===
Contributors: Wedgies, Michael Keating, James Barcellano
Donate link: http://wedgies.com
Tags: survey, shortcode, poll
Requires at least: 2.5
Tested up to: 3.8
Stable tag: 1.1
Stable tag: 1.2

Wedgies are polls you can embed on your Wordpress page. Engage your audience by asking them a question via Wedgies.

== Description ==

Polls are one of the best ways to engage your website audience. Wedgies are well designed and responsive polling widgets that look great anywhere on your website, allow you to engage your visitors with questions, and encourage them to share their votes back out to social media.
The Wedgies social polling WordPress plug-in allows you to easily add social polls to your Wordpress site, post, or page.

Social polls are a great way to engage site visitors and are proven as one of the best ways to create engagement.

On average, 1 in 5 readers will vote on a poll, 1 in 6 voters will share their poll results to social media, which creates 20% more site traffic for the publisher of the poll.

We're always happy to get you setup and started, for questions email - info@wedgies.com

This plugin makes it easy to embed a [wedgie](http://wedgies.com) into the content of a post or page using a URL.

`http://www.wedgies.com/question/5164b8cc9688ec020000000a`

== Installation ==

1. Install the Wedgies Shortcode plugin.
1. Install the Wedgies Social Polling plugin.
2. Activate it through the "Plugins" menu in WordPress
3. Edit the post or page where you'd like to insert an embedded wedgie.
4. You can embed a wedgie one of two ways:
Expand All @@ -32,6 +38,9 @@ This plugin makes it easy to embed a [wedgie](http://wedgies.com) into the conte

== Changelog ==

= 1.2 =
* Updated Readme

= 1.1 =
* Update Readme
* Added ability to embed via URL
Expand Down
154 changes: 112 additions & 42 deletions wedgies-shortcode.php
@@ -1,18 +1,19 @@
<?php
/**
* @package Wedgies_Shortcode
* @version 1.1
* @version 1.2
*/
/*
Plugin Name: Wedgies Shortcode
Plugin Name: Social Polls by Wedgies.com
Plugin URI: http://wedgies.com
Description: Wedgies are polls you can embed on your Wordpress page. Engage your audience by asking them a question via Wedgies.
Version: 1.1
Description: Wedgies are polls you can embed on your WordPress page. Engage your audience by asking them a question via Wedgies.
Version: 1.2
Author: Brendan Nee, James Barcellano
Author URI: http://bn.ee
License: GPL3
*/
/*
Wedgies (Wordpress Plugin)
Wedgies (WordPress Plugin)
Copyright (C) 2013 Wedgies
Contact me at http://wedgies.com

Expand All @@ -30,52 +31,121 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

function enqueue_wedgie() {
wp_enqueue_script('wedgie_embed', 'https://www.wedgies.com/js/widgets.js', null, '1.00');
}
class Wedgie_Shortcode_Plugin {

add_shortcode("wedgie", "wedgie_handler");
const SHORTCODE_SLUG = 'wedgie';

function wedgie_handler($attrs) {
/**
* @var Wedgie_Shortcode_Plugin
*/
static protected $instance;

$attrs = shortcode_atts(array(
"id" => "52dc9862da36f6020000000c"
), $attrs);

$wedgie_output = '<noscript><a href="https://www.wedgies.com/question/' . $attrs['id'] . '">Vote on our poll!</a></noscript><div class="wedgie-widget" wd-pending wd-type="embed" wd-version="v1" id="' . $attrs['id'] . '" style="max-width: 720px;"></div>';

return $wedgie_output;
}
/**
* @return Wedgie_Shortcode_Plugin
*/
static public function instance() {
if ( empty( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}

wp_embed_register_handler( 'wedgie', '#http(s?)://(www\.)?wedgies\.com/question/(.*)#i', 'wp_embed_handler_wedgie', 1);
/**
* Add hooks
*/
protected function __construct() {
add_shortcode( self::SHORTCODE_SLUG, array( $this, 'shortcode_handler' ) );
wp_embed_register_handler( 'wedgie', '#http(s?)://(www\.)?wedgies\.com/question/(.*)#i', array( $this, 'wp_embed_handler' ), 1 );
add_action( 'the_posts', array( $this, 'conditionally_enqueue_script' ) );

function wp_embed_handler_wedgie( $matches, $attr, $url, $rawattr ) {

$embed = sprintf(
'<noscript><a href="https://www.wedgies.com/question/%1$s">Vote on our poll!</a></noscript><div class="wedgie-widget" wd-pending wd-type="embed" wd-version="v1" id="%1$s" style="max-width: 720px;"></div>', $matches[3]);
}

return apply_filters( 'embed_wedgie', $embed, $matches, $attr, $url, $rawattr );
}
/**
* wp_enqueue_scripts hook.
*/
public function enqueue_script() {
wp_enqueue_script( 'wedgie_embed', 'https://www.wedgies.com/js/widgets.js', null, '1.2' );
}

function has_wedgie($posts) {
if (empty($posts)) {
return $posts;
/**
* Construct a wedgie embed from an ID
*
* @param $id
* @return string
*/
function construct_embed( $id ) {
$embed = sprintf(
'<noscript><a href="%s">%s</a></noscript><div class="wedgie-widget" wd-pending wd-type="embed" wd-version="v1" id="%s" style="max-width: 720px;"></div>',
esc_url( 'https://www.wedgies.com/question/' . $id ),
esc_html__( 'Vote on our poll!', 'wedgies-shortcode' ),
esc_attr( $id )
);
return $embed;
}

$shortcode_found = false;

foreach ($posts as $post) {
if ( !(stripos($post->post_content, '[wedgie') === false) || preg_match('#http(s?)://(www\.)?wedgies\.com/question/(.*)#i', $post->post_content)) {
$shortcode_found = true;
break;
}

/**
* Shortcode handler for [wedgie]
*
* @param array $attrs
*
* @return string
*/
function shortcode_handler( $attrs ) {
$attrs = shortcode_atts(
array(
'id' => '52dc9862da36f6020000000c',
),
$attrs,
self::SHORTCODE_SLUG
);
$wedgie_output = $this->construct_embed( $attrs['id'] );
return $wedgie_output;
}

if ($shortcode_found) {
add_action('wp_enqueue_scripts', 'enqueue_wedgie');

/**
* Embed handler for Wedgie
*
* @param array $matches
* @param array $attr
* @param string $url
* @param string $rawattr
*
* @return mixed|void
*/
function wp_embed_handler( $matches, $attr, $url, $rawattr ) {
$embed = $this->construct_embed( $matches[3] );
return apply_filters( 'embed_wedgie', $embed, $matches, $attr, $url, $rawattr );
}

return $posts;


/**
* Hook handler for the_posts to enqueue the JS if a wedgie is found.
*
* @param array $posts
*
* @return mixed
*/
function conditionally_enqueue_script( $posts ) {
if ( empty( $posts ) ) {
return $posts;
}

$shortcode_found = false;

foreach ( $posts as $post ) {
if ( ! ( false === stripos( $post->post_content, '[' . self::SHORTCODE_SLUG ) ) || preg_match( '#http(s?)://(www\.)?wedgies\.com/question/(.*)#i', $post->post_content ) ) {
$shortcode_found = true;
break;
}
}

if ( $shortcode_found ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_script' ) );
}

return $posts;
}

}

add_action('the_posts', 'has_wedgie');
Wedgie_Shortcode_Plugin::instance();