From d6c9c4a55e64d15ee0b6863ed36393ad0f20222d Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 13 Feb 2022 11:10:15 -0600 Subject: [PATCH] Save post meta (#55) * save post meta --- classes/LBRY_Admin.php | 27 ++++++++++++++++++++---- classes/LBRY_Network.php | 44 ++++++++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 844072a..7829a00 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -117,7 +117,7 @@ class LBRY_Admin add_settings_field( LBRY_LBC_PUBLISH, 'LBC Per Publish', - array( $this, 'lbc_publish_callback' ), + array( $this, 'lbc_per_publish_callback' ), LBRY_ADMIN_PAGE, LBRY_SETTINGS_SECTION_GENERAL ); @@ -275,6 +275,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( + '

Set Default to always Publish to LBRY, this can be adjusted when publishing a New Post.

', + 'lbry_default_publish_setting', + LBRY_SETTINGS, + + ); + } + /** * Checkbox to default to always allow publish on LBRY */ @@ -355,13 +373,14 @@ class LBRY_Admin /** * Prints LBC per publish input */ - public function lbc_publish_callback() + public function lbc_per_publish_callback() { printf( - '', + '

Current minimum bid 0.001

', LBRY_LBC_PUBLISH, LBRY_SETTINGS, - $this->options[LBRY_LBC_PUBLISH] + $this->options[LBRY_LBC_PUBLISH], + plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png' ); } diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index 5d88d04..54564a2 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -37,7 +37,7 @@ class LBRY_Network add_action( 'add_meta_boxes', array( $this, 'lbry_meta_boxes' ) ); // 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 ); } /** @@ -64,20 +64,26 @@ class LBRY_Network */ public function save_post_meta( $post_id, $post ) { - if ($post->post_type != 'post') { - return; + if ( $post->post_type != 'post' ) { + return $post_id; + } + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + return $post_id; } - // 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; } - - // Check if the current user has permission to edit the post. - $post_type = get_post_type_object($post->post_type); - 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; } + 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]; $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]; // 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 ( $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 ($new_channel !== $cur_channel) { - update_post_meta($post_id, LBRY_POST_CHANNEL, $new_channel); + if ( $license !== $cur_license ) { + update_post_meta( $post_id, LBRY_POST_PUB_LICENSE, $license ); } - - if ($will_publish && $post->post_status == 'publish') { + if ( ( $will_publish ) && ( $will_publish == 1 ) && $post->post_status == 'publish') { // 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 ); } }