Last active
September 18, 2020 20:35
-
-
Save birgire/e64f54b9bbbdd58a6ef1f45ce88068b1 to your computer and use it in GitHub Desktop.
WordPress Plugin: Ping screenshots capture generation using an hidden iframe only once for published posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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