2018-09-01 01:50:29 +02:00
< ? php
/**
* Class for connecting with the LBRY Network
*
* @ package LBRYPress
*/
2022-02-13 20:26:04 +01:00
defined ( 'ABSPATH' ) || die (); // Exit if accessed directly
2018-09-01 01:50:29 +02:00
class LBRY_Network
{
2018-10-12 03:21:49 +02:00
/**
* The Publishing Object
* @ var LBRY_Network_Publisher
*/
public $publisher = null ;
/**
* The Parsing Object
* @ var LBRY_Network_Parser
*/
public $parser = null ;
2018-09-11 22:10:15 +02:00
public function __construct ()
2018-09-01 01:50:29 +02:00
{
2018-10-12 03:21:49 +02:00
$this -> publisher = new LBRY_Network_Publisher ();
$this -> parser = new LBRY_Network_Parser ();
$this -> post_meta_setup ();
}
/**
* Sets up everything for the post meta boxes
*/
private function post_meta_setup ()
{
// Add the meta boxes
2022-02-13 18:08:24 +01:00
add_action ( 'add_meta_boxes' , array ( $this , 'lbry_meta_boxes' ) );
2018-10-12 03:21:49 +02:00
// Save the post meta on 'save_post' hook
2022-02-13 18:10:15 +01:00
add_action ( 'wp_insert_post' , array ( $this , 'save_post_meta' ), 11 , 2 );
2022-02-14 19:22:29 +01:00
2022-02-13 20:26:04 +01:00
// Checkbox inside the WordPres meta box near "Publish" button
add_action ( 'post_submitbox_misc_actions' , array ( $this , 'publish_to_lbry_checkbox' ) );
2018-10-12 03:21:49 +02:00
}
/**
* Adds the meta boxes to the post editing backend
*/
2022-02-13 18:08:24 +01:00
public function lbry_meta_boxes ( $post )
2018-10-12 03:21:49 +02:00
{
2018-11-01 18:13:55 +01:00
// IDEA: Support post types based on user selection
2018-10-12 03:21:49 +02:00
add_meta_box (
'lbry-network-publishing' , // Unique ID
2022-02-13 18:08:24 +01:00
__ ( 'LBRY Network' , 'lbrypress' ), // Title
2018-10-12 03:21:49 +02:00
array ( $this , 'meta_box_html' ), // Callback function
'post' , // Screen Options (or post type)
'side' , // Context
2018-10-12 05:59:01 +02:00
'high' // Priority
2018-10-12 03:21:49 +02:00
);
}
/**
* Handles saving the post meta that is relative to publishing to the LBRY Network
2018-10-12 22:31:23 +02:00
* @ param int $post_id The ID of the post we are saving
* @ param WP_Post $post The Post Object we are saving
2018-11-09 19:34:12 +01:00
* @ return int Returns post_id if user cannot edit post
2018-10-12 03:21:49 +02:00
*/
2022-02-12 22:28:25 +01:00
public function save_post_meta ( $post_id , $post )
2018-10-12 03:21:49 +02:00
{
2022-02-13 18:10:15 +01:00
if ( $post -> post_type != 'post' ) {
return $post_id ;
}
if ( defined ( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id ;
2018-11-01 18:13:55 +01:00
}
2018-10-12 05:59:01 +02:00
// Verify the nonce before proceeding.
2022-02-13 18:10:15 +01:00
if ( ! isset ( $_POST [ '_lbrynonce' ] ) || ! wp_verify_nonce ( $_POST [ '_lbrynonce' ], 'lbry_publish_post_nonce' ) ) {
//LBRY()->notice->set_notice('error', 'Security check failed' );
2018-10-12 05:59:01 +02:00
return $post_id ;
}
2022-02-13 18:10:15 +01:00
$post_type = get_post_type_object ( $post -> post_type );
if ( ! current_user_can ( $post_type -> cap -> edit_post , $post_id ) ) {
2018-10-12 05:59:01 +02:00
return $post_id ;
}
2022-02-13 18:10:15 +01:00
if ( ( $_POST [ LBRY_WILL_PUBLISH ] ) && $_POST [ LBRY_WILL_PUBLISH ] != get_post_meta ( $post_id , LBRY_WILL_PUBLISH , true ) ) {
update_post_meta ( $post_id , LBRY_WILL_PUBLISH , $_POST [ LBRY_WILL_PUBLISH ] );
} elseif ( ! isset ( $_POST [ LBRY_WILL_PUBLISH ] ) ) {
update_post_meta ( $post_id , LBRY_WILL_PUBLISH , 0 );
}
2018-10-12 05:59:01 +02:00
2022-02-13 17:39:42 +01:00
$channel = $_POST [ LBRY_POST_PUB_CHANNEL ];
2022-02-19 20:04:38 +01:00
$cur_channel = ( get_post_meta ( $post_id , LBRY_POST_PUB_CHANNEL , true ) ? get_post_meta ( $post_id , LBRY_POST_PUB_CHANNEL , true ) : get_post_meta ( $post_id , '_lbry_channel' , true ) );
2022-02-13 17:39:42 +01:00
$license = $_POST [ LBRY_POST_PUB_LICENSE ];
$cur_license = get_post_meta ( $post_id , LBRY_POST_PUB_LICENSE , true );
$will_publish = $_POST [ LBRY_WILL_PUBLISH ];
2018-10-12 05:59:01 +02:00
2018-10-12 22:31:23 +02:00
// Update meta acordingly
2022-02-13 18:10:15 +01:00
2022-02-19 20:04:38 +01:00
if ( ( $channel ) && ( $channel !== $cur_channel ) && ( $channel != null || $channel != '' ) ) {
2022-02-13 18:10:15 +01:00
update_post_meta ( $post_id , LBRY_POST_PUB_CHANNEL , $channel );
delete_post_meta ( $post_id , '_lbry_channel' ); // remove the _lbry_channel if already set from the post and replaces with _lbry_post_pub_channel to avoid confusion
} elseif ( $channel === $cur_channel && ( $cur_channel === get_post_meta ( $post_id , '_lbry_channel' , true ) ) ) {
update_post_meta ( $post_id , LBRY_POST_PUB_CHANNEL , $channel );
2022-02-19 20:04:38 +01:00
delete_post_meta ( $post_id , '_lbry_channel' ); // remove the _lbry_channel if already set from the post and replaces with _lbry_post_pub_channel to avoid confusion
2018-10-12 22:31:23 +02:00
}
2022-02-19 20:04:38 +01:00
if ( ( $license ) && ( $license !== $cur_license ) && ( $license != null || $license != '' ) ) {
update_post_meta ( $post_id , LBRY_POST_PUB_LICENSE , $license );
} elseif ( $license === $cur_license && ( $cur_license === get_post_meta ( $post_id , 'lbry_license' , true ) ) ) {
2022-02-13 18:10:15 +01:00
update_post_meta ( $post_id , LBRY_POST_PUB_LICENSE , $license );
2018-10-12 05:59:01 +02:00
}
2022-02-19 20:04:38 +01:00
$published = get_post_meta ( $post_id , '_lbry_is_published' , true );
$will_pub = get_post_meta ( $post_id , '_lbry_will_publish' , true );
if ( ( $will_pub == 0 ) && ( ! ( $published ) || ( $published ) == 0 ) ) {
// If not publishing to LBRY we want to not save unused values in database
delete_post_meta ( $post_id , '_lbry_post_pub_channel' );
delete_post_meta ( $post_id , '_lbry_post_pub_license' );
delete_post_meta ( $post_id , '_lbry_canonical_url' );
}
$pub_channel = ( $channel ) ? $channel : $cur_channel ;
$pub_license = ( $license ) ? $license : $cur_license ;
if ( ( $will_publish ) && ( $will_publish == true ) && ( $post -> post_status == 'publish' ) && ( $pub_channel ) && ( $pub_license ) && ( $pub_license != null ) ) {
2018-10-12 22:31:23 +02:00
// Publish the post on the LBRY Network
2022-02-19 20:04:38 +01:00
$this -> publisher -> publish ( $post , $pub_channel , $pub_license );
2018-10-12 22:31:23 +02:00
}
2018-10-12 03:21:49 +02:00
}
2022-02-13 20:26:04 +01:00
/**
* Creates a checkbox that changes the default setting to always publish to LBRY ,
* can be reverted individually to not publish on a per post basis . Saves to options table .
*/
public function publish_to_lbry_checkbox ( $post )
{
if ( get_post_type ( $post_id ) != 'post' ) {
return $post ;
}
2022-02-16 21:52:43 +01:00
$post_id = $post -> ID ;
$lbry_claim_id = get_post_meta ( $post_id , '_lbry_claim_id' , true );
if ( $_GET [ 'action' ] === 'edit' ) {
if ( get_post_meta ( $post_id , '_lbry_canonical_url' , true ) == null || empty ( get_post_meta ( $post_id , '_lbry_canonical_url' , true ) ) ) {
2022-02-19 20:04:38 +01:00
$canonical_url = LBRY () -> daemon -> canonical_url ( $lbry_claim_id );
if ( $canonical_url != null ) {
update_post_meta ( $post_id , '_lbry_canonical_url' , $canonical_url );
} elseif ( $canonical_url == null ) {
delete_post_meta ( $post_id , '_lbry_canonical_url' );
}
2022-02-16 21:52:43 +01:00
}
}
2022-02-19 20:04:38 +01:00
$lbry_url = ( get_post_meta ( $post_id , '_lbry_canonical_url' , true ) );
2022-02-16 21:52:43 +01:00
if ( $lbry_url ) {
$open_url = str_replace ( 'lbry://' , 'open.lbry.com/' , $lbry_url );
}
2022-02-19 20:04:38 +01:00
$lbry_published = get_post_meta ( $post_id , '_lbry_is_published' , true );
$will_publish = get_post_meta ( $post_id , '_lbry_will_publish' , true );
$lbry_post_pub_channel = get_post_meta ( $post_id , '_lbry_post_pub_channel' , true );
if ( ( $will_publish ) ? $will_publish : $will_publish = get_option ( LBRY_SETTINGS )[ 'lbry_default_publish_setting' ] );
if ( ( $_GET [ 'action' ] === 'edit' ) ? $value = $lbry_published : $value = $will_publish );
$checked = checked ( $value , true , false );
2022-02-13 20:26:04 +01:00
// nonce set on page meta-box.php
2022-02-19 20:04:38 +01:00
if ( ( ( $will_publish == true ) && ( $lbry_post_pub_channel ) ) || ( ( ( $lbry_published ) || ( $lbry_claim_id ) ) && ( ( $lbry_url != null ) || ( $lbry_claim_id ) ) ) ) {
2022-02-16 21:52:43 +01:00
printf (
' < hr class = " lbry-hr-meta " >
< div class = " misc-pub-section lbry-meta-published-lbry-wrapper " >
2022-02-19 20:04:38 +01:00
< span class = " lbry-pub-metabox " >< img src = " ' . __( '%1 $s ', 'lbrypress' ) . ' " class = " icon icon-lbry meta-icon-lbry " ></ span > < span class = " post-lbry-display-before " > Published on :</ span > < span class = " post-lbry-display " >< strong > LBRY </ strong ></ span >
2022-02-16 21:52:43 +01:00
</ div >
< div class = " misc-pub-section lbry-url-meta-wrapper " >
< span class = " lbry-meta-label " >< strong > LBRY URL : </ strong ></ span >< a href = " ' . esc_url( '%2 $s ', 'lbrypress' ) . ' " target = " _blank " > ' . esc_html__( ' % 3 $s ', ' lbrypress ' ) . ' </ a >
</ div >
< div class = " misc-pub-section lbry-claim-id-meta-wrapper " >
< span class = " lbry-meta-label " >< strong > LBRY claim ID : </ strong ></ span >< span class = " lbry-pub-metabox " >< p class = " lbry-claim-id-metabox " > ' . esc_html__( ' % 4 $s ', ' lbrypress ' ) . ' </ p ></ span >
</ div >
< div class = " misc-pub-section lbry-meta-checkbox-wrapper lbry-meta-wrapper-last " >
2022-02-19 20:04:38 +01:00
< span class = " lbry-meta-label " > Update Post on : < span class = " post-lbry-display " >< strong > LBRY </ strong ></ span > < input type = " checkbox " class = " lbry-meta-checkbox " value = " 1 " ' . esc_attr(' % 5 $s ') . ' name = " ' . esc_attr('%6 $s ') . ' " >
2022-02-16 21:52:43 +01:00
</ div > ' ,
plugin_dir_url ( LBRY_PLUGIN_FILE ) . 'admin/images/lbry.png' ,
$open_url ,
$lbry_url ,
$lbry_claim_id ,
2022-02-19 20:04:38 +01:00
$checked ,
2022-02-16 21:52:43 +01:00
LBRY_WILL_PUBLISH
);
} else {
printf (
' < div class = " misc-pub-section lbry-meta-checkbox-wrapper lbry-meta-wrapper-last " >
2022-02-19 20:04:38 +01:00
< span class = " lbry-pub-metabox " >< img src = " ' . __( '%1 $s ', 'lbrypress' ) . ' " class = " icon icon-lbry meta-icon-lbry " ></ span >< span class = " lbry-meta-label " > Publish to : < span class = " post-lbry-display " > < strong > LBRY </ strong ></ span > < input type = " checkbox " class = " lbry-meta-checkbox " value = " 1 " ' . esc_attr(' % 2 $s ') . ' name = " ' . esc_attr('%3 $s ') . ' " >
2022-02-16 21:52:43 +01:00
</ div > ' ,
plugin_dir_url ( LBRY_PLUGIN_FILE ) . 'admin/images/lbry.png' ,
2022-02-19 20:04:38 +01:00
$checked ,
2022-02-16 21:52:43 +01:00
LBRY_WILL_PUBLISH
);
}
2022-02-13 20:26:04 +01:00
}
2018-10-12 22:31:23 +02:00
/**
* Returns the HTML for the LBRY Meta Box
2018-11-09 19:34:12 +01:00
* @ param WP_POST $post
2018-10-12 22:31:23 +02:00
*/
2022-02-12 22:28:25 +01:00
public function meta_box_html ( $post )
2018-10-12 03:21:49 +02:00
{
2022-02-13 18:08:24 +01:00
require_once ( LBRY_ABSPATH . 'templates/meta-box.php' );
2018-09-01 01:50:29 +02:00
}
}