Created
February 24, 2017 06:37
-
-
Save imath/24d014cacb78f4654de0d3aff3778fb4 to your computer and use it in GitHub Desktop.
Anonymise Idea authors in WP Idea Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Exit if accessed directly | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* Référencer un Idea Meta ! | |
*/ | |
function uana_custom_register_metas() { | |
// A very simple way to add a custom field (for text field only) | |
wp_idea_stream_ideas_register_meta( 'anonymous', array( | |
'label' => __( 'Post anonymously', 'uana-custom-domain' ), | |
'form' => 'uana_custom_form_display', // fonction de rappel pour le formulaire en front | |
'single' => 'uana_custom_single_display', // fonction de rappel pour l'affichage de l'idée dans sa vue unique. | |
) ); | |
} | |
add_action( 'wp_idea_stream_init', 'uana_custom_register_metas' ); | |
/** | |
* fonction de rappel pour le formulaire en front. | |
* | |
* @param string $display_meta Le contenu à afficher. | |
* @param object $meta_object Un object contenant { | |
* meta_key l'identifiant de l'Idea Meta. | |
* field_name l'attribut name du champ. | |
* field_value la valeur du champ. | |
* label le texte à utiliser comme label du champ. | |
* } | |
* @return string HTML Output. | |
*/ | |
function uana_custom_form_display( $display_meta = '', $meta_object = null ) { | |
if ( empty( $meta_object->field_name ) ) { | |
return; | |
} | |
printf( ' | |
<p> | |
<label for="_wp_idea_stream_%1$s"> | |
<input type="checkbox" name="%2$s" id="_wp_idea_stream_the_category_%1$s" value="1" %3$s/> | |
%4$s | |
</label> | |
</p>', | |
esc_attr( $meta_object->meta_key ), | |
esc_attr( $meta_object->field_name ), | |
checked( 1, (int) $meta_object->field_value, false ), | |
esc_html( $meta_object->label ) | |
); | |
} | |
/** | |
* fonction de rappel pour l'affichage de l'idée dans sa vue unique. | |
* | |
* @param string $display_meta Le contenu à afficher. | |
* @param object $meta_object Un object contenant { | |
* meta_key l'identifiant de l'Idea Meta. | |
* field_name l'attribut name du champ. | |
* field_value la valeur du champ. | |
* label le texte à utiliser comme label du champ. | |
* } | |
* @return string HTML Output. | |
*/ | |
function uana_custom_single_display( $display_meta = '', $meta_object = null ) { | |
// Bail if no field name or user don't want to be anonymous. | |
if ( empty( $meta_object->field_name ) || 1 !== (int) $meta_object->field_value ) { | |
return; | |
} | |
?> | |
<p class="description"> | |
<small><?php esc_html_e( 'PS: The author of this idea prefers to stay anonymous.' ); ?></small> | |
</p> | |
<?php | |
} | |
/** | |
* Fonction pour masquer l'avatar de l'auteur de l'idée. | |
* | |
* @param string $avatar_link Avatar + lien vers le profil de l'auteur de l'idée. | |
* @param integer $author Identifiant de l'auteur. | |
* @param string $avatar Url de l'avatar | |
* @param WP_Post $idea L'objet idée. | |
* @return string HTML Output. | |
*/ | |
function uana_ideas_archive_anonymous_avatar( $avatar_link = '', $author = 0, $avatar = '', $idea = null ) { | |
if ( empty( $idea->ID ) ) { | |
return ''; | |
} | |
if ( 1 !== (int) wp_idea_stream_ideas_get_meta( $idea->ID, 'anonymous' ) ) { | |
return $avatar_link; | |
} | |
return '<div class="dashicons dashicons-editor-help" style="font-size: 100px; color: #ccc"></div>'; | |
} | |
add_filter( 'wp_idea_stream_ideas_get_author_avatar', 'uana_ideas_archive_anonymous_avatar', 10, 4 ); | |
/** | |
* Fonction pour masquer le lien vers le profil de l'utilisateur | |
* du footer de l'idée. | |
* | |
* @param string $idea_footer Le contenu du footer de l'idée. | |
* @param array $retarray Un tableau associatif contenant les parties de ce footer | |
* @param WP_Post $idea L'objet idée. | |
* @param array $placeholders Un tableau associatif contenant les valeurs à utiliser pour | |
* les éléments du $retarray (ex: liste des catégories). | |
* @return string Le contenu original, ou modifié, du footer de l'idée. | |
*/ | |
function uana_ideas_footer_anonymous_user_link( $idea_footer = '', $retarray = array(), $idea = null, $placeholders = array() ) { | |
if ( empty( $idea->ID ) ) { | |
return ''; | |
} | |
// In this cases, the user link doesn't appear | |
if ( ! wp_idea_stream_is_single_idea() && ! wp_idea_stream_ideas_has_featured_image() ) { | |
return $idea_footer; | |
} | |
if ( is_super_admin() || 1 !== (int) wp_idea_stream_ideas_get_meta( $idea->ID, 'anonymous' ) ) { | |
return $idea_footer; | |
} | |
// Init some vars | |
$category_list = $tag_list = ''; | |
// Translators: 1 is category, 2 is tag and 3 is the date. | |
$retarray['utility_text'] = _x( 'This idea was posted on %3$s.', 'default idea footer utility text', 'wp-idea-stream' ); | |
if ( ! empty( $placeholders['category'] ) ) { | |
// Translators: 1 is category, 2 is tag and 3 is the date. | |
$retarray['utility_text'] = _x( 'This idea was posted in %1$s on %3$s.', 'idea attached to at least one category footer utility text', 'wp-idea-stream' ); | |
$category_list = $placeholders['category']; | |
} | |
if ( ! empty( $placeholders['tag'] ) ) { | |
// Translators: 1 is category, 2 is tag and 3 is the date. | |
$retarray['utility_text'] = _x( 'This idea was tagged %2$s on %3$s.', 'idea attached to at least one tag footer utility text', 'wp-idea-stream' ); | |
$tag_list = $placeholders['tag']; | |
if ( ! empty( $placeholders['category'] ) ) { | |
// Translators: 1 is category, 2 is tag and 3 is the date. | |
$retarray['utility_text'] = _x( 'This idea was posted in %1$s and tagged %2$s on %3$s.', 'idea attached to at least one tag and one category footer utility text', 'wp-idea-stream' ); | |
} | |
} | |
$retarray['utility_text'] = sprintf( | |
$retarray['utility_text'], | |
$category_list, | |
$tag_list, | |
$placeholders['date'] | |
); | |
return join( ' ', $retarray ); | |
} | |
add_filter( 'wp_idea_stream_ideas_get_idea_footer', 'uana_ideas_footer_anonymous_user_link', 10, 4 ); | |
/** | |
* Enrichit la requête principale de WordPress d'une Meta Query | |
* lorsqu'un utilisateur consulte le profil d'un autre utilisateur | |
* de sorte à retirer les idées que l'utilisateur consulté a publié | |
* anonymement. | |
* | |
* @param WP_Query $q la requête principale de WordPress. | |
*/ | |
function uana_users_profile_hide_posted_anonymously( WP_Query $q ) { | |
// Only do something when viewing displayed user ideas | |
if ( wp_idea_stream_is_user_profile() && wp_idea_stream_is_user_profile_ideas() ) { | |
// Don't do anything if user is looking at himself, or if user is an admin | |
if ( wp_idea_stream_is_current_user_profile() || is_super_admin() ) { | |
return; | |
} | |
// Set a not exists meta query for the anonymous post meta | |
$q->set( 'meta_query', array( | |
array( | |
'key' => '_ideastream_anonymous', | |
'compare' => 'NOT EXISTS' | |
) | |
) ); | |
} | |
} | |
add_action( 'wp_idea_stream_parse_query', 'uana_users_profile_hide_posted_anonymously', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment