initial commit
[danixland-author-signature.git] / danixland-author-signature.php
CommitLineData
e8ea8794 1<?php
2
3defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
4
5/*
6Plugin Name: danixland author signature
7Description: Enqueue a signature on your posts and pages.
8Plugin URI: http://danixland.net/?p=3694
9Version: 1.1
10Author: Danilo 'danix' Macr&igrave;
11Author URI: http://danixland.net
12License: GPL2
13License URI: https://www.gnu.org/licenses/gpl-2.0.html
14Text Domain: dnxasi
15
16*/
17
18/**
19 * Add plugin i18n domain: dnxasi
20 * @since 0.3
21 */
22load_plugin_textdomain('dnxasi', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
23
24/**
25 * Let's load our helper file ( utility functions )
26 * @since 1.1
27 */
28require( dirname( __FILE__ ) . '/inc/dnxasi_helper.php' );
29
30if ( is_admin()) {
31 require_once( dirname(__FILE__) . '/inc/dnxasi-settings.php' );
32}
33
34// enqueue our js for the modal uploader
35function dnxasi_enqueue_scripts($hook) {
36 wp_register_script( 'dnxasi_uploader_modal', plugins_url('/js/dnxasi_uploader_modal.js', __FILE__) );
37 $translation = array(
38 'frameTitle' => __('Select your signature', 'dnxasi'),
39 'buttonText' => __('Use Image', 'dnxasi'),
40 'placeholder' => plugins_url('/img/placeholder.png', __FILE__)
41 );
42 if ( current_user_can( 'publish_posts' ) ) {
43 if ( 'profile.php' != $hook ) {
44 return;
45 }
46 wp_enqueue_media();
47 wp_localize_script( 'dnxasi_uploader_modal', 'data', $translation );
48 wp_enqueue_script( 'dnxasi_uploader_modal', false , array('jquery'), '0.1' );
49 }
50}
51add_action('admin_enqueue_scripts', 'dnxasi_enqueue_scripts');
52
53/**
54 * Adds additional user fields
55 * more info: http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
56 */
57function dnxasi_add_signature( $user ) {
58 if ( ! current_user_can('publish_posts') )
59 return false;
60
61 $imagesrc = get_the_author_meta( 'dnxasi_meta_signature', $user->ID );
62 $imagesrc = ( ! empty($imagesrc) ) ? $imagesrc : plugins_url('/img/placeholder.png', __FILE__);
63?>
64
65 <h2><?php _e( 'Profile signature', 'dnxasi' ); ?></h2>
66
67 <p><?php _e('if you don\'t select a signature image nothing will be displayed in the content of your posts or pages.', 'dnxasi'); ?></p>
68
69 <table class="form-table">
70
71 <tr>
72 <th><label for="dnxasi_meta_signature"><?php _e( 'add your signature image', 'dnxasi' ); ?></label></th>
73 <td>
74 <img id="dnxasi_signature_preview" src="<?php echo esc_url( $imagesrc ); ?>" style="width:450px"><br />
75 <!-- Outputs the text field and displays the URL of the image retrieved by the media uploader -->
76 <input type="text" name="dnxasi_meta_signature" id="dnxasi_meta_signature" value="<?php echo esc_url_raw( get_the_author_meta( 'dnxasi_meta_signature', $user->ID ) ); ?>" class="regular-text" readonly="readonly">
77 <!-- Outputs the save button -->
78 <input type='button' class="button-secondary" value="<?php _e( 'Select Signature', 'dnxasi' ); ?>" id="dnxasi_uploadimage">
79 <!-- Outputs the reset button -->
80 <input type='button' class="button-secondary" value="<?php _e( 'Remove Signature', 'dnxasi' ); ?>" id="dnxasi_deleteimage"><br>
81 <span class="description"><?php _e( 'Select and preview your signature image.', 'dnxasi' ); ?></span>
82 </td>
83 </tr>
84
85 </table><!-- end form-table -->
86<?php } // dnxasi_add_signature
87add_action( 'show_user_profile', 'dnxasi_add_signature' );
88add_action( 'edit_user_profile', 'dnxasi_add_signature' );
89
90/**
91* Saves additional user fields to the database
92*/
93function dnxasi_save_signature( $user_id ) {
94
95 // only saves if the current user can edit user profiles
96 if ( ! current_user_can( 'edit_user', $user_id ) )
97 return false;
98
99 update_usermeta( $user_id, 'dnxasi_meta_signature', $_POST['dnxasi_meta_signature'] );
100}
101
102add_action( 'personal_options_update', 'dnxasi_save_signature' );
103add_action( 'edit_user_profile_update', 'dnxasi_save_signature' );
104
105/**
106* Display the signature image only for authors who set it in the admin panel
107* and only in single view.
108*/
109function dnxasi_display_content($content) {
110 global $page;
111 $options = get_option('dnxasi_settings');
112 $signature = get_the_author_meta('dnxasi_meta_signature');
113 $alignment = ( ! empty($options['dnxasi_signature_position']) ) ? $options['dnxasi_signature_position'] : 'right';
114 $signature_size = ( ! empty($options['dnxasi_signature_size']) ) ? $options['dnxasi_signature_size'] : '300';
115 if ( is_single() && is_main_query() && ( '' != $signature ) ) {
116 if ( ! dnxasi_is_post_paginated() || ( $page === dnxasi_post_last_page() ) ) {
117 $new_content = '<figure class="dnxasi_signature"><img class="dnxasi_signature_image align' . $alignment . '" src="' . $signature . '" width="' . $signature_size . '" alt="' . get_the_author_meta('display_name') . '"></figure>';
118 $content .= $new_content;
119 }
120 }
121 return $content;
122}
123add_filter( 'the_content', 'dnxasi_display_content' );