Default license selector #51

Merged
lemsmyth merged 3 commits from default-license-select into master 2022-02-13 17:39:42 +01:00
8 changed files with 130 additions and 129 deletions
Showing only changes of commit bf183aac5a - Show all commits

View file

@ -64,7 +64,7 @@ class LBRYPress
*/
public static function instance()
{
if (is_null(self::$_instance)) {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
@ -77,7 +77,7 @@ class LBRYPress
public function __construct()
{
$this->define_constants();
spl_autoload_register(array($this, 'lbry_autoload_register'));
spl_autoload_register( array( $this, 'lbry_autoload_register' ) );
$this->init();
$this->init_hooks();
}
@ -88,10 +88,10 @@ class LBRYPress
* @param string $name Constant name.
* @param string|bool $value Constant value.
*/
private function define($name, $value)
private function define( $name, $value )
{
if (! defined($name)) {
define($name, $value);
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
@ -130,11 +130,11 @@ class LBRYPress
/**
* Autoloader Registration
*/
private function lbry_autoload_register($class)
private function lbry_autoload_register( $class )
{
$file_name = LBRY_ABSPATH . 'classes/' . $class . '.php';
if (file_exists($file_name)) {
if ( file_exists( $file_name ) ) {
require $file_name;
return;
}
@ -149,7 +149,7 @@ class LBRYPress
$this->speech = new LBRY_Speech();
// Admin request
if (is_admin()) {
if ( is_admin() ) {
$this->admin = new LBRY_Admin();
$this->notice = new LBRY_Admin_Notice();
$this->network = new LBRY_Network();
@ -161,15 +161,15 @@ class LBRYPress
*/
private function init_hooks()
{
register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate'));
register_deactivation_hook(LBRY_PLUGIN_FILE, array($this, 'deactivate'));
register_activation_hook( LBRY_PLUGIN_FILE, array( $this, 'activate' ) );
register_deactivation_hook( LBRY_PLUGIN_FILE, array( $this, 'deactivate' ) );
// Banner output for published posts
// NOTE: move this to its own class to reduce clutter?
add_action('the_content', array($this, 'published_on_lbry_banner'), 12, 1);
add_action( 'the_content', array( $this, 'published_on_lbry_banner' ), 12, 1 );
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('lbry-css', plugins_url('/frontend/lbry.css', LBRY_PLUGIN_FILE));
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_style( 'lbry-css', plugins_url( '/frontend/lbry.css', LBRY_PLUGIN_FILE ) );
});
}
@ -181,16 +181,16 @@ class LBRYPress
// TODO: Make sure errors are thrown if daemon can't be contacted, stop activation
// Add options to the options table we need
if (! get_option(LBRY_SETTINGS)) {
if (! get_option( LBRY_SETTINGS ) ) {
// Default options
//Default options
$option_defaults = array(
LBRY_LICENSE => $this->licenses[0],
LBRY_LBC_PUBLISH => 1
);
add_option(LBRY_SETTINGS, $option_defaults, false);
add_option( LBRY_SETTINGS, $option_defaults, false );
}
if ( ! get_option( LBRY_SPEECH_SETTINGS ) ) {
@ -221,26 +221,26 @@ class LBRYPress
public function deactivate()
{
// TODO: Stop the daemon
error_log('Deactivated LBRYPress');
error_log( 'Deactivated LBRYPress' );
}
public function published_on_lbry_banner($content)
{
if (!is_single() || !in_the_loop() || !is_main_query()) {
if ( ! is_single() || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
global $post;
if ($post->post_type != 'post') {
if ( $post->post_type != 'post' ) {
return $content;
}
if (!get_post_meta($post->ID, LBRY_WILL_PUBLISH, true)) {
if ( ! get_post_meta( $post->ID, LBRY_WILL_PUBLISH, true ) ) {
return $content;
}
ob_start();
require(LBRY_ABSPATH . 'templates/published_on_lbry_banner.php');
require( LBRY_ABSPATH . 'templates/published_on_lbry_banner.php' );
$banner = ob_get_clean();
return $content .= $banner;

View file

@ -26,11 +26,11 @@ class LBRY_Admin
public function create_options_page()
{
add_menu_page(
__('LBRYPress Settings', 'lbrypress'),
__('LBRYPress', 'lbrypress'),
__( 'LBRYPress Settings', 'lbrypress' ),
__( 'LBRYPress', 'lbrypress' ),
'manage_options',
LBRY_ADMIN_PAGE,
array($this, 'options_page_html'),
array( $this, 'options_page_html' ),
plugin_dir_url(LBRY_PLUGIN_FILE) . '/admin/images/lbry-logo.svg'
);
@ -246,7 +246,7 @@ class LBRY_Admin
{
// Get first available account address from Daemon
$address = LBRY()->daemon->address_list();
$address = is_array($address) && !empty($address) ? $address[0]->address : '';
$address = is_array( $address ) && ! empty( $address ) ? $address[0]->address : '';
printf(
'<input type="text" id="%1$s" name="%2$s[%1$s]" value="%3$s" readonly />',
LBRY_WALLET,
@ -264,11 +264,11 @@ class LBRY_Admin
$options = '';
// Create options list, select current license
//
foreach (LBRY()->licenses as $value => $name) {
foreach ( LBRY()->licenses as $value => $name ) {
$selected = $this->options[LBRY_LICENSE] === $value;
$options .= '<option value="' . $value . '"';
if ($selected) {
if ( $selected ) {
$options .= ' selected';
}
$options .= '>'. $name . '</option>';
@ -398,7 +398,7 @@ class LBRY_Admin
if (!get_transient('lbry_wallet_warning_email')) {
$email = get_option('admin_email');
$subject = 'Your LBRYPress Wallet Balance is Low!';
$message = "You LBRY Wallet for your wordpress installation at " . site_url() . " is running very low.\r\n\r\nYou currently have " . $balance . ' LBC left in your wallet. In order to keep publishing to the LBRY network, please add some LBC to your account.';
$message = "Your LBRY Wallet for your WordPress installation at " . site_url() . " is running very low.\r\n\r\nYou currently have " . $balance . ' LBC left in your wallet. In order to keep publishing to the LBRY network, please add some LBC to your account.';
wp_mail($email, $subject, $message);
set_transient('lbry_wallet_warning_email', true, DAY_IN_SECONDS);
}

View file

@ -7,9 +7,10 @@
class LBRY_Admin_Notice
{
public function __construct()
{
add_action('admin_notices', array($this, 'admin_notices'));
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}
/**
@ -17,12 +18,12 @@ class LBRY_Admin_Notice
*/
public function admin_notices()
{
if (get_transient('lbry_notices')) {
$notices = get_transient('lbry_notices');
foreach ($notices as $key => $notice) {
$this->create_admin_notice($notice);
if ( get_transient( 'lbry_notices' ) ) {
$notices = get_transient( 'lbry_notices' );
foreach ( $notices as $key => $notice ) {
$this->create_admin_notice( $notice );
}
delete_transient('lbry_notices');
delete_transient( 'lbry_notices' );
}
}
@ -30,7 +31,7 @@ class LBRY_Admin_Notice
* Sets transients for admin errors
*/
// TODO: Make sure we only set one transient at a time per error
public function set_notice($status = 'error', $message = 'Something went wrong', $is_dismissible = false)
public function set_notice( $status = 'error', $message = 'Something went wrong', $is_dismissible = false )
{
$notice = array(
'status' => $status,
@ -38,24 +39,24 @@ class LBRY_Admin_Notice
'is_dismissible' => $is_dismissible
);
if (! get_transient('lbry_notices')) {
set_transient('lbry_notices', array($notice));
if (! get_transient( 'lbry_notices' ) ) {
set_transient( 'lbry_notices', array( $notice ) );
} else {
$notices = get_transient('lbry_notices');
$notices = get_transient( 'lbry_notices' );
$notices[] = $notice;
set_transient('lbry_notices', $notices);
set_transient( 'lbry_notices', $notices );
}
}
/**
* Prints an admin notice
*/
private function create_admin_notice($notice)
private function create_admin_notice( $notice )
{
$class = 'notice notice-' . $notice['status'];
if ($notice['is_dismissible']) {
if ( $notice['is_dismissible'] ) {
$class .= ' is-dismissible';
}
printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($notice['message']));
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $notice['message'] ) );
}
}

View file

@ -35,11 +35,11 @@ class LBRY_Daemon
public function wallet_unused_address()
{
try {
$result = $this->request('address_unused');
$result = $this->request( 'address_unused' );
return $result->result;
} catch (LBRYDaemonException $e) {
$this->logger->log('address_unused error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue getting unused address.');
} catch ( LBRYDaemonException $e ) {
$this->logger->log( 'address_unused error', $e->getMessage() . ' | Code: ' . $e->getCode() );
LBRY()->notice->set_notice( 'error', 'Issue getting unused address.' );
return;
}
}
@ -50,19 +50,18 @@ class LBRY_Daemon
* @param int $page Pagination page number
* @return array Array of address lists linked to this account
*/
public function address_list($page = 1)
{
public function address_list( $page = 1 ) {
// Get 20 per page
$params = array(
'page' => $page,
'page_size' => 20
);
try {
$result = $this->request('address_list', $params);
$result = $this->request( 'address_list', $params );
return $result->result->items;
} catch (LBRYDaemonException $e) {
$this->logger->log('address_list error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue getting address list.');
} catch ( LBRYDaemonException $e ) {
$this->logger->log( 'address_list error', $e->getMessage() . ' | Code: ' . $e->getCode() );
LBRY()->notice->set_notice( 'error', 'Issue getting address list.' );
return;
}
}
@ -71,13 +70,13 @@ class LBRY_Daemon
* Returns the available balance of a current LBRY account
* https://lbry.tech/api/sdk#wallet_balance
* @param string $address Wallet Address
* @return array $wallet_balance Wallet Balance
* @return object $wallet_balance Wallet Balance
*
*/
public function wallet_balance()
{
{
try { // Convert JSON string to an object
$result = $this->request('wallet_balance');
$result = $this->request( 'wallet_balance' );
return $result;
} catch (LBRYDaemonException $e) {
$this->logger->log('wallet_balance error', $e->getMessage() . ' | Code: ' . $e->getCode());
@ -92,7 +91,7 @@ class LBRY_Daemon
* @param int $page Pagination page number
* @return array claim dictionary or null if empty
*/
public function channel_list($page = 1)
public function channel_list( $page = 1 )
{
$params = array(
'page' => $page,
@ -100,11 +99,11 @@ class LBRY_Daemon
);
try {
$result = $this->request('channel_list', $params)->result->items;
return empty($result) ? null : $result;
} catch (LBRYDaemonException $e) {
$this->logger->log('channel_list error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue retrieving channel list.');
$result = $this->request( 'channel_list', $params )->result->items;
return empty( $result ) ? null : $result;
} catch ( LBRYDaemonException $e ) {
$this->logger->log( 'channel_list error', $e->getMessage() . ' | Code: ' . $e->getCode() );
LBRY()->notice->set_notice( 'error', 'Issue retrieving channel list.' );
return;
}
}
@ -114,17 +113,18 @@ class LBRY_Daemon
* https://lbry.tech/api/sdk#channel_create
* @return array dictionary containing result of the request
*/
public function channel_new($channel_name, $channel_bid)
public function channel_new( $channel_name, $channel_bid )
{
// TODO: Sanitize channel name and bid
// Make sure no @ sign, as we will add that
if (strpos($channel_name, '@')) {
throw new \Exception('Illegal character "@" in channel name', 1);
if ( strpos( $channel_name, '@' ) ) {
throw new \Exception( 'Illegal character "@" in channel name', 1 );
}
// No white space allowed
if (strpos($channel_name, ' ')) {
throw new \Exception("No spaces allowed in channel name", 1);
if ( strpos( $channel_name, ' ' ) ) {
throw new \Exception( "No spaces allowed in channel name", 1 );
}
$channel_name = '@' . $channel_name;
@ -142,8 +142,8 @@ class LBRY_Daemon
return $result->result;
} catch (LBRYDaemonException $e) {
$this->logger->log('channel_new error', $e->getMessage() . ' | Code: ' . $e->getCode());
throw new \Exception('Issue creating new channel.', 1);
$this->logger->log( 'channel_new error', $e->getMessage() . ' | Code: ' . $e->getCode() );
throw new \Exception( 'Issue creating new channel.', 1 );
return;
}
}
@ -153,9 +153,9 @@ class LBRY_Daemon
* @param string $claim_id
* @return string Canonical URL, null if not found
*/
public function canonical_url($claim_id = null)
public function canonical_url( $claim_id = null )
{
if (!$claim_id) {
if ( ! $claim_id ) {
return null;
}
@ -169,13 +169,13 @@ class LBRY_Daemon
);
$items = $result->result->items;
if (!$items) {
if ( ! $items ) {
return null;
}
return $items[0]->canonical_url;
} catch (LBRYDaemonException $e) {
$this->logger->log('canonical_url error', $e->getMessage() . ' | Code: ' . $e->getCode());
} catch ( LBRYDaemonException $e ) {
$this->logger->log( 'canonical_url error', $e->getMessage() . ' | Code: ' . $e->getCode() );
return;
}
}
@ -190,18 +190,18 @@ class LBRY_Daemon
*
* @return object $result
*/
public function publish($args)
public function publish( $args )
{
try {
$result = $this->request(
'publish',
$args
);
$this->logger->log('publish success!', 'Successfully published post with result: ' . print_r($result->result, true));
$this->logger->log( 'publish success!', 'Successfully published post with result: ' . print_r( $result->result, true ) );
return $result->result;
} catch (LBRYDaemonException $e) {
$this->logger->log('publish error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.');
} catch ( LBRYDaemonException $e ) {
$this->logger->log('publish error', $e->getMessage() . ' | Code: ' . $e->getCode() );
LBRY()->notice->set_notice( 'error', 'Issue publishing / updating post to LBRY Network.' );
return;
}
}
@ -212,52 +212,52 @@ class LBRY_Daemon
* @param array $params The Parameters to send the LBRY API Call
* @return string The cURL response
*/
private function request($method, $params = array())
private function request( $method, $params = array() )
{
// JSONify our request data
$data = array(
'method' => $method,
'params' => $params
);
$data = json_encode($data);
$data = json_encode( $data );
// Send it via curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->address);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_URL, $this->address );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) );
curl_setopt( $ch, CURLOPT_AUTOREFERER, false );
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
$result = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
$result = curl_exec( $ch );
$response_code = curl_getinfo( $ch, CURLINFO_RESPONSE_CODE );
curl_close( $ch );
if ($response_code != '200') {
$this->logger->log("Damon Connection Issue", "Daemon connection returned response code $response_code");
throw new LBRYDaemonException("Daemon Connection Issue", $response_code);
if ( $response_code != '200' ) {
$this->logger->log( "Damon Connection Issue", "Daemon connection returned response code $response_code" );
throw new LBRYDaemonException( "Daemon Connection Issue", $response_code );
}
$result = json_decode($result);
$this->check_for_errors($result);
$result = json_decode( $result );
$this->check_for_errors( $result );
return $result;
}
/**
* Checks for erros in decoded daemon response and throws an exception if it finds one
* Checks for errors in decoded daemon response and throws an exception if it finds one
* @param $response
*/
private function check_for_errors($response)
private function check_for_errors( $response )
{
if (property_exists($response, 'error')) {
if ( property_exists( $response, 'error' ) ) {
$message = $response->error->message;
$code = $response->error->code;
$this->logger->log("Daemon error code $code", $message);
throw new LBRYDaemonException($message, $code);
$this->logger->log( "Daemon error code $code", $message );
throw new LBRYDaemonException( $message, $code );
}
}

View file

@ -7,6 +7,7 @@
class LBRY_Daemon_Logger
{
/**
* The directory to log to
* @var string

View file

@ -62,7 +62,7 @@ class LBRY_Network
* @param WP_Post $post The Post Object we are saving
* @return int Returns post_id if user cannot edit post
*/
public function save_post_meta($post_id, $post)
public function save_post_meta( $post_id, $post )
{
if ($post->post_type != 'post') {
return;
@ -103,7 +103,7 @@ class LBRY_Network
* Returns the HTML for the LBRY Meta Box
* @param WP_POST $post
*/
public function meta_box_html($post)
public function meta_box_html( $post )
{
require_once(LBRY_ABSPATH . 'templates/meta_box.php');
}

View file

@ -17,44 +17,44 @@ class LBRY_Network_Publisher
{
// Get converted markdown into a file
$filepath = LBRY_ABSPATH . 'tmp/' . $post->post_name . time() . '.md';
$file = fopen($filepath, 'w');
$converted = LBRY()->network->parser->convert_to_markdown($post);
$write_status = $file && fwrite($file, $converted);
fclose($file);
$file = fopen( $filepath, 'w' );
$converted = LBRY()->network->parser->convert_to_markdown( $post );
$write_status = $file && fwrite( $file, $converted );
fclose( $file );
try {
if (!$write_status) {
throw new \Exception('Write Status failure', 1);
throw new \Exception( 'Write Status failure', 1 );
}
// If everything went well with the conversion, carry on
$args = array(
'name' => $post->post_name,
'bid' => number_format(floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]), 2, '.', ''),
'bid' => number_format( floatval( get_option( LBRY_SETTINGS )[LBRY_LBC_PUBLISH] ), 3, '.', '' ),
'file_path' => $filepath,
'title' => $post->post_title,
'languages' => array(substr(get_locale(), 0, 2)),
'languages' => array( substr( get_locale(), 0, 2 ) ),
'license' => get_option(LBRY_SETTINGS)[LBRY_LICENSE]
);
// Setup channel
if ($channel && $channel != 'null') {
if ( $channel && $channel != 'null' ) {
$args['channel_id'] = $channel;
}
// Setup featured image
$featured_id = get_post_thumbnail_id($post);
$featured_image = wp_get_attachment_image_src($featured_id, 'medium');
$featured_id = get_post_thumbnail_id( $post );
$featured_image = wp_get_attachment_image_src( $featured_id, 'medium' );
if ($featured_image[0]) {
if ( $featured_image[0] ) {
$args['thumbnail_url'] = $featured_image[0];
}
// Setup Tags
$tags = get_the_terms( $post, 'post_tag' );
if ($tags) {
if ( $tags ) {
$tag_names = [];
foreach ($tags as $tag) {
foreach ( $tags as $tag ) {
$tag_names[] = $tag->name;
}
$args['tags'] = $tag_names;
@ -62,35 +62,35 @@ class LBRY_Network_Publisher
// Build description using Yoast if installed and its used, excerpt/title otherwise
$description = false;
if (class_exists('WPSEO_META')) {
$description = WPSEO_META::get_value('opengraph-description', $post->ID);
if ( class_exists( 'WPSEO_META' ) ) {
$description = WPSEO_META::get_value( 'opengraph-description', $post->ID );
}
if (!$description) {
$excerpt = get_the_excerpt($post);
if ( ! $description ) {
$excerpt = get_the_excerpt( $post );
$description = $excerpt ? $excerpt : $post->post_title;
}
$description .= ' | Originally published at ' . get_permalink($post);
$description .= ' | Originally published at ' . get_permalink( $post );
$args['description'] = $description;
$result = LBRY()->daemon->publish($args);
$result = LBRY()->daemon->publish( $args );
$outputs = $result->outputs;
if ($outputs && is_array($outputs)) {
if ( $outputs && is_array( $outputs ) ) {
$output = $result->outputs[0];
$claim_id = $output->claim_id;
// Set Claim ID
update_post_meta($post->ID, LBRY_CLAIM_ID, $claim_id);
update_post_meta( $post->ID, LBRY_CLAIM_ID, $claim_id );
// Set Canonical URL
$canonical_url = LBRY()->daemon->canonical_url($claim_id);
update_post_meta($post->ID, LBRY_CANONICAL_URL, $canonical_url);
$canonical_url = LBRY()->daemon->canonical_url( $claim_id );
update_post_meta( $post->ID, LBRY_CANONICAL_URL, $canonical_url );
}
} catch (Exception $e) {
error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage());
error_log( 'Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage() );
} finally {
//Delete the temporary markdown file
unlink($filepath);
unlink( $filepath );
}
}
}

View file

@ -45,4 +45,3 @@ $channel_list = $LBRY->daemon->channel_list();
?>
</form>
</div><!-- wrap -->