Save post meta #55

Merged
lemsmyth merged 8 commits from save-post-meta into master 2022-02-13 18:10:15 +01:00
5 changed files with 160 additions and 93 deletions

View file

@ -120,8 +120,8 @@ class LBRYPress
$this->define('LBRY_LBC_PUBLISH', 'lbry_lbc_publish'); // amount of lbc to use per publish $this->define('LBRY_LBC_PUBLISH', 'lbry_lbc_publish'); // amount of lbc to use per publish
$this->define('LBRY_WILL_PUBLISH', '_lbry_will_publish'); // The meta key for if to publish to LBRY Network or not $this->define('LBRY_WILL_PUBLISH', '_lbry_will_publish'); // The meta key for if to publish to LBRY Network or not
$this->define('LBRY_POST_CHANNEL', '_lbry_channel'); // The meta key for which channel to publish $this->define('LBRY_POST_CHANNEL', '_lbry_channel'); // The meta key for which channel to publish
$this->define('LBRY_POST_PUB_CHANNEL', '_lbry_post_pub_channel'); // The meta key for which channel to publish $this->define('LBRY_POST_PUB_CHANNEL', '_lbry_post_pub_channel'); // The meta key for which channel to publish on
$this->define('LBRY_POST_POST_LICENSE', '_lbry_post_pub_license'); // The meta key for which license to publish on $this->define('LBRY_POST_PUB_LICENSE', '_lbry_post_pub_license'); // The meta key for which license to publish on
$this->define('LBRY_CLAIM_ID', '_lbry_claim_id'); // The Claim ID for the post as it was published on LBRY $this->define('LBRY_CLAIM_ID', '_lbry_claim_id'); // The Claim ID for the post as it was published on LBRY
$this->define('LBRY_CANONICAL_URL', '_lbry_canonical_url'); // The canonical url for the published lbry post $this->define('LBRY_CANONICAL_URL', '_lbry_canonical_url'); // The canonical url for the published lbry post
$this->define('LBRY_SPEECH_ASSET_URL', 'speech_asset_url'); // The meta key for an asset's speech url $this->define('LBRY_SPEECH_ASSET_URL', 'speech_asset_url'); // The meta key for an asset's speech url

View file

@ -37,7 +37,7 @@ class LBRY_Admin
// Admin stylesheet enqueue // Admin stylesheet enqueue
function load_admin_stylesheet( $hook ) { function load_admin_stylesheet( $hook ) {
if ( ( $_GET['page'] == 'lbrypress' ) ) { if ( ( $hook == 'post.php' ) || ( $hook == 'post-new.php' ) || ( $_GET['page'] == 'lbrypress' ) ) {
wp_enqueue_style( wp_enqueue_style(
'lbry-admin', 'lbry-admin',
plugins_url( '/admin/css/lbry-admin.css', LBRY_PLUGIN_FILE ), plugins_url( '/admin/css/lbry-admin.css', LBRY_PLUGIN_FILE ),
@ -90,6 +90,14 @@ class LBRY_Admin
LBRY_SETTINGS_SECTION_GENERAL LBRY_SETTINGS_SECTION_GENERAL
); );
add_settings_field(
'lbry_default_publish_setting',
'Always Publish to LBRY',
array( $this, 'lbry_always_pub_callback' ),
LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL
);
add_settings_field( add_settings_field(
'default_lbry_channel', 'default_lbry_channel',
'Default Publish Channel', 'Default Publish Channel',
@ -97,6 +105,7 @@ class LBRY_Admin
LBRY_ADMIN_PAGE, LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL LBRY_SETTINGS_SECTION_GENERAL
); );
add_settings_field( add_settings_field(
LBRY_LICENSE, LBRY_LICENSE,
'LBRY Publishing License', 'LBRY Publishing License',
@ -108,7 +117,7 @@ class LBRY_Admin
add_settings_field( add_settings_field(
LBRY_LBC_PUBLISH, LBRY_LBC_PUBLISH,
'LBC Per Publish', 'LBC Per Publish',
array( $this, 'lbc_publish_callback' ), array( $this, 'lbc_per_publish_callback' ),
LBRY_ADMIN_PAGE, LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL LBRY_SETTINGS_SECTION_GENERAL
); );
@ -170,22 +179,24 @@ class LBRY_Admin
public function sanitize_general_settings( $input ) public function sanitize_general_settings( $input )
{ {
if (!empty($input[LBRY_SPEECH_CHANNEL])) { $new_input = get_option( LBRY_SETTINGS ); // get saved data
$channel = $input[LBRY_SPEECH_CHANNEL];
$channel = str_replace('@', '', $channel);
$input[LBRY_SPEECH_CHANNEL] = $channel;
}
if (!empty($input[LBRY_SPEECH_PW])) { if ( isset( $input[LBRY_WALLET] ) ) {
$encrypted = $this->encrypt($input['lbry_speech_pw']); $new_input[LBRY_WALLET] = sanitize_text_field( $input[LBRY_WALLET] );
$input[LBRY_SPEECH_PW] = $encrypted;
} else {
// If we have a password and its empty, keep orginal password
if (!empty(get_option(LBRY_SETTINGS)[LBRY_SPEECH_PW])) {
$input[LBRY_SPEECH_PW] = get_option(LBRY_SETTINGS)[LBRY_SPEECH_PW];
} }
$new_input['lbry_default_publish_setting'] = $input['lbry_default_publish_setting'];
if ( isset( $input['default_lbry_channel'] ) ) {
$new_input['default_lbry_channel'] = sanitize_text_field( $input['default_lbry_channel'] );
} }
return $input; $license_array = LBRY()->licenses;
if ( isset( $input[LBRY_LICENSE] ) && ( in_array( $input[LBRY_LICENSE], $license_array ) ) ) {
$new_input[LBRY_LICENSE] = sanitize_text_field( $input[LBRY_LICENSE] );
}
if ( isset( $input[LBRY_LBC_PUBLISH] ) ) {
$new_input[LBRY_LBC_PUBLISH] = number_format( floatval( $input[LBRY_LBC_PUBLISH] ), 3, '.', '' );
}
return $new_input;
} }
public function sanitize_speech_settings( $input ) public function sanitize_speech_settings( $input )
@ -219,13 +230,6 @@ class LBRY_Admin
{ {
print 'This is where you can configure how LBRYPress will distribute your content:'; print 'This is where you can configure how LBRYPress will distribute your content:';
} }
/**
* Section info for the Speech Channel Section
*/
public function speech_section_callback()
{
print 'If you have a Spee.ch account, you can enter your account details here, if you don\'t already have a Spee.ch account, no need to enter anything here.';
}
/** /**
* Section info for the Available Channel(s) Section * Section info for the Available Channel(s) Section
@ -245,6 +249,15 @@ class LBRY_Admin
<?php } <?php }
} }
/**
* Section info for the Speech Channel Section
*/
public function speech_section_callback()
{
print 'If you have a Spee.ch account, you can enter your account details here, if you don\'t already have a Spee.ch account, no need to enter anything here.';
}
/** /**
* Prints Wallet input * Prints Wallet input
*/ */
@ -261,6 +274,24 @@ class LBRY_Admin
); );
} }
/**
* Checkbox to default to always allow publish on LBRY
*/
public function lbry_always_pub_callback()
{
$options = get_option( LBRY_SETTINGS )['lbry_default_publish_setting'];
if ( ! isset( $options ) ) {
$options = 0;
}
$checked = checked( $options, 1, false );
printf(
'<input type="checkbox" id="lbry_default_publish_setting" name="' . esc_attr('%2$s[%1$s]') . '" value="1" ' . esc_attr( $checked ) . '><p>Set Default to always Publish to <strong>LBRY</strong>, this can be adjusted when publishing a New Post.</p>',
'lbry_default_publish_setting',
LBRY_SETTINGS,
);
}
/** /**
* Prints select to choose a default to publish to channel * Prints select to choose a default to publish to channel
*/ */
@ -323,16 +354,17 @@ class LBRY_Admin
/** /**
* Prints LBC per publish input * Prints LBC per publish input
*/ */
public function lbc_publish_callback() public function lbc_per_publish_callback()
{ {
printf( printf(
'<input type="number" id="%1$s" name="%2$s[%1$s]" value="%3$s" min="0.01" step="0.01"/>', '<input type="number" id="' . esc_attr('%1$s') . '" name="' . esc_attr('%2$s[%1$s]') . '" value="' . esc_attr('%3$.3f') . '" min="0.001" step="0.001"><p>Current minimum bid <img src="' . esc_attr('%4$s ') . '" class="icon icon-lbc bid-icon-lbc"> 0.001</p>',
LBRY_LBC_PUBLISH, LBRY_LBC_PUBLISH,
LBRY_SETTINGS, LBRY_SETTINGS,
$this->options[LBRY_LBC_PUBLISH] $this->options[LBRY_LBC_PUBLISH],
plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png'
); );
}
}
/** /**
* Channels Page * Channels Page

View file

@ -34,21 +34,21 @@ class LBRY_Network
private function post_meta_setup() private function post_meta_setup()
{ {
// Add the meta boxes // Add the meta boxes
add_action('add_meta_boxes', array($this, 'add_meta_boxes')); add_action( 'add_meta_boxes', array( $this, 'lbry_meta_boxes' ) );
// Save the post meta on 'save_post' hook // Save the post meta on 'save_post' hook
add_action('wp_insert_post', array($this, 'save_post_meta'), 11, 2); add_action( 'wp_insert_post', array( $this, 'save_post_meta' ), 11, 2 );
} }
/** /**
* Adds the meta boxes to the post editing backend * Adds the meta boxes to the post editing backend
*/ */
public function add_meta_boxes() public function lbry_meta_boxes( $post )
{ {
// IDEA: Support post types based on user selection // IDEA: Support post types based on user selection
add_meta_box( add_meta_box(
'lbry-network-publishing', // Unique ID 'lbry-network-publishing', // Unique ID
'LBRY Network', // Title __('LBRY Network', 'lbrypress'), // Title
array($this, 'meta_box_html'), // Callback function array($this, 'meta_box_html'), // Callback function
'post', // Screen Options (or post type) 'post', // Screen Options (or post type)
'side', // Context 'side', // Context
@ -64,20 +64,26 @@ class LBRY_Network
*/ */
public function save_post_meta( $post_id, $post ) public function save_post_meta( $post_id, $post )
{ {
if ($post->post_type != 'post') { if ( $post->post_type != 'post' ) {
return; return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
} }
// Verify the nonce before proceeding. // Verify the nonce before proceeding.
if (!isset($_POST['_lbrynonce']) || !wp_verify_nonce($_POST['_lbrynonce'], 'lbry_publish_channels')) { if ( ! isset( $_POST['_lbrynonce'] ) || ! wp_verify_nonce( $_POST['_lbrynonce'], 'lbry_publish_post_nonce' ) ) {
//LBRY()->notice->set_notice('error', 'Security check failed' );
return $post_id; return $post_id;
} }
$post_type = get_post_type_object( $post->post_type );
// Check if the current user has permission to edit the post. if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
$post_type = get_post_type_object($post->post_type);
if (!current_user_can($post_type->cap->edit_post, $post_id)) {
return $post_id; return $post_id;
} }
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 );
}
$channel = $_POST[LBRY_POST_PUB_CHANNEL]; $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 ) ); $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 ) );
@ -86,18 +92,20 @@ class LBRY_Network
$will_publish = $_POST[LBRY_WILL_PUBLISH]; $will_publish = $_POST[LBRY_WILL_PUBLISH];
// Update meta acordingly // Update meta acordingly
if (!$will_publish) {
update_post_meta($post_id, LBRY_WILL_PUBLISH, 'false');
} else {
update_post_meta($post_id, LBRY_WILL_PUBLISH, 'true');
}
if ($new_channel !== $cur_channel) {
update_post_meta($post_id, LBRY_POST_CHANNEL, $new_channel);
}
if ($will_publish && $post->post_status == 'publish') { 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
}
if ( $license !== $cur_license ) {
update_post_meta( $post_id, LBRY_POST_PUB_LICENSE, $license );
}
if ( ( $will_publish ) && ( $will_publish == 1 ) && $post->post_status == 'publish') {
// Publish the post on the LBRY Network // Publish the post on the LBRY Network
$this->publisher->publish($post, get_post_meta($post_id, LBRY_POST_CHANNEL, true)); $this->publisher->publish( $post, $channel, $license );
} }
} }
@ -107,6 +115,6 @@ class LBRY_Network
*/ */
public function meta_box_html( $post ) public function meta_box_html( $post )
{ {
require_once(LBRY_ABSPATH . 'templates/meta_box.php'); require_once( LBRY_ABSPATH . 'templates/meta-box.php' );
} }
} }

69
templates/meta-box.php Normal file
View file

@ -0,0 +1,69 @@
<?php
/**
* ============================
* META BOX FOR POST PAGE
* Prints the post meta box
* @package LBRYPress
* ============================
*/
defined('ABSPATH') || die(); // Exit if accessed directly
$unnatributed = (object) array(
'name' => 'none (anonymous)',
'claim_id' => 'null'
);
// Generate a custom nonce
$lbrynonce = wp_create_nonce( 'lbry_publish_post_nonce' );
$channels = LBRY()->daemon->channel_list();
$channels[] = $unnatributed;
$post_id = $post->ID;
$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 ) );
$default_channel = get_option( LBRY_SETTINGS )['default_lbry_channel'];
// Sort the channels in a natural way
usort( $channels, array( 'LBRYPress', 'channel_name_comp' ) );
?>
<section>
<input type="hidden" id="_lbrynonce" name="_lbrynonce" value="<?php echo $lbrynonce ?>">
<div><label for="LBRY_POST_PUB_CHANNEL" class="lbry-meta-bx-label lbry-meta-bx-channel"><?php esc_html_e( 'Channel to Publish: ', 'lbrypress' ); ?></label></div><?php
$options = '';
if ( $channels ) {
foreach ( $channels as $index=>$channel ) {
$options .= '<option class="lbry-meta-bx-option lbry-meta-option-channel" value="' . esc_attr( $channel->claim_id ) . '"';
if ( isset( $cur_channel ) ? $cur_channel : $cur_channel = $default_channel ) {
$options .= selected( $cur_channel, $channel->claim_id, false );
}
$options .= '>' . esc_html__( $channel->name, 'lbrypress' ) . '</option>';
}
printf(
'<select id="' . esc_attr('%1$s') . '" name="' . esc_attr('%1$s') . '">' . esc_html('%2$s') . '</select>',
LBRY_POST_PUB_CHANNEL,
$options
);
} ?>
<div><label for="LBRY_POST_PUB_LICENSE" class="lbry-meta-bx-label lbry-meta-bx-license"><?php esc_html_e( 'Publish License: ', 'lbrypress' ); ?></label></div><?php
$licenses = LBRY()->licenses;
$options = '';
$default_license = get_option(LBRY_SETTINGS)[LBRY_LICENSE];
$cur_license = get_post_meta( $post_id, LBRY_POST_PUB_LICENSE, true );
// Create options list, select current license
if ( $licenses ) {
foreach ( $licenses as $value => $name ) {
$options .= '<option class="lbry-meta-bx-option lbry-meta-bx-option-last lbry-meta-option-license" value="' . esc_attr( $value ) . '"';
if ( isset( $cur_license ) ? $cur_license : $cur_license = $default_license ) {
$options .= selected( $cur_license, $value, false );
}
$options .= '>'. esc_html__( $name, 'lbrypress' ) . '</option>';
}
}
printf(
'<select class="" id="'.esc_attr('%1$s').'" name="'. esc_attr('%1$s') .'">' . esc_html('%2$s') . '</select>',
LBRY_POST_PUB_LICENSE,
$options
);
?>
</section>

View file

@ -1,42 +0,0 @@
<?php
$unnatributed = (object) array(
'name' => 'none (anonymous)',
'claim_id' => 'null'
);
$channels = LBRY()->daemon->channel_list();
$channels[] = $unnatributed;
// Sort the channels in a natural way
usort($channels, array('LBRYPress', 'channel_name_comp'));
$cur_channel = get_post_meta($post->ID, LBRY_POST_CHANNEL, true);
$will_publish = get_post_meta($post->ID, LBRY_WILL_PUBLISH, true);
?>
<?php wp_nonce_field('lbry_publish_channels', '_lbrynonce'); ?>
<div class="lbry-meta-checkbox-wrapper">
<label class="lbry-meta-label">
<input type="checkbox" class="lbry-meta-checkbox" name="<?= LBRY_WILL_PUBLISH ?>" value="true"
<?php
if ($will_publish === 'true' || $will_publish === '') {
echo 'checked';
}
?>
>
Sync this post on channel:
</label>
</div>
<select class="lbry-meta-select" name="<?= LBRY_POST_CHANNEL ?>">
<?php foreach ($channels as $index=>$channel): ?>
<option value="<?= $channel->claim_id ?>"
<?php
if ($cur_channel) {
if ($cur_channel === $channel->claim_id) {
echo 'selected';
}
} elseif ($index === 0) {
echo 'selected';
}
?>
>
<?= $channel->name ?>
</option>
<?php endforeach; ?>
</select>