From bea3a63ff4cffd564d91cd6113bcc30091529077 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 8 Nov 2018 08:50:29 -0500 Subject: [PATCH] Add blog icon setting --- includes/admin/settings.php | 58 +++++++++++++++++++++++++++++++++---- includes/init.php | 4 +++ includes/server/actors.php | 7 ++--- js/icon-upload.js | 28 ++++++++++++++++++ 4 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 js/icon-upload.js diff --git a/includes/admin/settings.php b/includes/admin/settings.php index bdb1973..04e7a71 100644 --- a/includes/admin/settings.php +++ b/includes/admin/settings.php @@ -12,11 +12,19 @@ function register_settings_sections() { 'description' => __( "The site's description in the Fediverse", 'pterotype' ), 'show_in_rest' => true, ) ); + \register_setting( 'pterotype_settings', 'pterotype_blog_icon', array( + 'type' => 'string', + 'description' => __( "The URL of the site's icon in the Fediverse", 'pterotype' ), + 'show_in_rest' => true, + ) ); \add_settings_section( 'pterotype_identity', - 'Fediverse Identity', + __( 'Fediverse Identity', 'pterotype' ), function() { - ?>

These settings determine how your blog will look in other Fediverse apps

' . __( + 'These settings determine how your blog will look in other Fediverse apps', + 'pterotype' + ) . '

'; }, 'pterotype' ); @@ -25,9 +33,8 @@ function register_settings_sections() { function register_settings_fields() { \add_settings_field( 'pterotype_blog_name', - 'Site Name', + __( 'Site Name', 'pterotype' ), function() { - // TODO fill this with the existing option or the site default if not set ?> true, @@ -52,6 +59,36 @@ function register_settings_fields() { 'pterotype', 'pterotype_identity' ); + \add_settings_field( + 'pterotype_blog_icon', + __( 'Site Icon', 'pterotype' ), + function() { + \wp_enqueue_media(); + \wp_enqueue_script( + 'pterotype_media_script', + \plugin_dir_url( __FILE__ ) . '../../js/icon-upload.js' + ); + ?> +
+ +
+ + + diff --git a/includes/init.php b/includes/init.php index 718ee56..d1fac39 100644 --- a/includes/init.php +++ b/includes/init.php @@ -89,6 +89,10 @@ add_action( 'update_option_pterotype_blog_description', function() { \pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG ); } ); +add_action( 'update_option_pterotype_blog_icon', function() { + \pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG ); +} ); + add_action( 'admin_menu', function() { \pterotype\admin\register_admin_page(); \pterotype\settings\register_settings_sections(); diff --git a/includes/server/actors.php b/includes/server/actors.php index 89babf7..ed48832 100644 --- a/includes/server/actors.php +++ b/includes/server/actors.php @@ -169,10 +169,9 @@ function get_blog_actor() { 'publicKeyPem' => \pterotype\pgp\get_public_key( $actor_id ), ), ); - if ( has_custom_logo() ) { - $actor['icon'] = make_icon_array( - wp_get_attachment_image_src( get_theme_mod( 'custom_logo' ) )[0] - ); + $icon = \pterotype\settings\get_blog_icon_value(); + if ( $icon && ! empty( $icon ) ) { + $actor['icon'] = make_icon_array( $icon ); } return $actor; } diff --git a/js/icon-upload.js b/js/icon-upload.js new file mode 100644 index 0000000..7608239 --- /dev/null +++ b/js/icon-upload.js @@ -0,0 +1,28 @@ +jQuery(document).ready(function($) { + var frame; + + $('#pterotype_blog_icon_button').on('click', function(event) { + event.preventDefault(); + + if (frame) { + frame.open(); + return; + } + + frame = wp.media({ + title: 'Select an image', + button: { + text: 'Use this image' + }, + multiple: false + }); + + frame.on('select', function() { + var attachment = frame.state().get('selection').first().toJSON(); + $('#pterotype_blog_icon_image').attr('src', attachment.url); + $('#pterotype_blog_icon').attr('value', attachment.url); + }); + + frame.open(); + }); +});