Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Plugin: Ping screenshots capture generation using an hidden iframe only once for published posts
<?php
/**
* Plugin Name: Ping Screenshots Capture
* Description: Ping screenshots capture generation using an hidden iframe only once for published posts
* Version: 1.0.0
* Author: birgire
*/
namespace PingScreenshotCapture;
add_action( 'admin_init', function()
{
$main = new Main;
// Adjust to your needs
$main
->set_src( 'http://example.tld' )
->init();
} );
/**
* Main Class
*/
class Main
{
/**
* @val string
*/
private $src;
/**
* Set source
*/
public function set_src( $src )
{
$this->src = $src;
return $this;
}
/**
* Init
*/
public function init()
{
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
}
/**
* Add the meta box.
*/
public function add_metabox()
{
add_meta_box(
'ping-screenshot-capture-meta-box',
__( 'Ping Screenshot Capture', 'psc' ),
array( $this, 'render' ),
'post',
'advanced',
'default'
);
}
/**
* Render the meta box.
*/
public function render( $post )
{
$pinged = get_post_meta( $post->ID, 'pinged_screenshot_capture', true );
if( empty( $pinged )
&& 'publish' === get_post_status( $post )
)
{
// Ping
$out = sprintf(
'<p>%s</p><iframe style="visibility: hidden; height: 0; width: 0; border: none; " src="%s"></iframe>',
esc_html__( "Let's Ping", 'psc' ),
esc_url( $this->src )
);
// Record that we've just pinged
update_post_meta( $post->ID, 'pinged_screenshot_capture', 'done' );
}
elseif( ! empty( $pinged ) )
{
$out = esc_html__( 'Already Pinged', 'psc' );
}
else
{
$out = esc_html__( 'Will be pinged during publish', 'psc' );
}
// Display
printf( '<div>%s</div>', $out );
}
} // end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment