lbrypress/classes/LBRY_Network.php

121 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/**
* Class for connecting with the LBRY Network
*
* @package LBRYPress
*/
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;
public function __construct()
{
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 00:26:37 +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 14:54:26 +01:00
add_action( 'wp_insert_post', array( $this, 'save_post_meta' ), 11, 2 );
2018-10-12 03:21:49 +02:00
}
/**
* Adds the meta boxes to the post editing backend
*/
2022-02-13 00:26:37 +01:00
public function lbry_meta_boxes( $post )
2018-10-12 03:21:49 +02: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 00:26:37 +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
'high' // Priority
2018-10-12 03:21:49 +02:00
);
}
/**
* Handles saving the post meta that is relative to publishing to the LBRY Network
* @param int $post_id The ID of the post we are saving
* @param WP_Post $post The Post Object we are saving
* @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 14:54:26 +01:00
if ( $post->post_type != 'post' ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Verify the nonce before proceeding.
2022-02-13 14:54:26 +01:00
if ( ! isset( $_POST['_lbrynonce'] ) || ! wp_verify_nonce( $_POST['_lbrynonce'], 'lbry_publish_post_nonce' ) ) {
//LBRY()->notice->set_notice('error', 'Security check failed' );
return $post_id;
}
2022-02-13 14:54:26 +01:00
$post_type = get_post_type_object( $post->post_type );
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
return $post_id;
}
2022-02-13 14:54:26 +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 );
}
2022-02-12 22:59:23 +01:00
$channel = $_POST[LBRY_POST_PUB_CHANNEL];
$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-12 22:46:20 +01:00
$license = $_POST[LBRY_POST_PUB_LICENSE];
2022-02-12 22:59:23 +01:00
$cur_license = get_post_meta( $post_id, LBRY_POST_PUB_LICENSE, true );
$will_publish = $_POST[LBRY_WILL_PUBLISH];
// Update meta acordingly
2022-02-13 14:54:26 +01:00
if ( $channel !== $cur_channel ) {
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 );
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
}
2022-02-13 14:54:26 +01:00
if ( $license !== $cur_license ) {
update_post_meta( $post_id, LBRY_POST_PUB_LICENSE, $license );
}
2022-02-13 14:54:26 +01:00
if ( ( $will_publish ) && ( $will_publish == 1 ) && $post->post_status == 'publish') {
// Publish the post on the LBRY Network
2022-02-13 14:54:26 +01:00
$this->publisher->publish( $post, $channel, $license );
}
2018-10-12 03:21:49 +02:00
}
/**
* Returns the HTML for the LBRY Meta Box
* @param WP_POST $post
*/
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 00:26:37 +01:00
require_once( LBRY_ABSPATH . 'templates/meta-box.php' );
}
}