$client_id,
'post_types' => $post_types,
);
}
add_action(
'admin_init',
function () {
register_setting(
'livere_comments',
LIVERE_COMMENTS_OPTION,
array(
'type' => 'array',
'sanitize_callback' => 'livere_comments_sanitize_settings',
'default' => array(
'client_id' => '',
'post_types' => array( 'post' ),
),
)
);
add_settings_section( 'livere_comments_main', '', '__return_false', 'livere-comments' );
add_settings_field(
'livere_comments_client_id',
__( 'Client ID', 'livere-comments' ),
function () {
$settings = livere_comments_get_settings();
printf(
'',
esc_attr( LIVERE_COMMENTS_OPTION ),
esc_attr( $settings['client_id'] )
);
echo '
';
echo esc_html__( 'Find it in the LiveRe admin, in the installation code: the value of client-id. You can also paste the whole installation code here.', 'livere-comments' );
echo '
';
},
'livere-comments',
'livere_comments_main',
array( 'label_for' => 'livere_comments_client_id' )
);
add_settings_field(
'livere_comments_post_types',
__( 'Show on', 'livere-comments' ),
function () {
$settings = livere_comments_get_settings();
echo '';
foreach ( livere_comments_available_post_types() as $name => $label ) {
printf(
' %4$s ',
esc_attr( LIVERE_COMMENTS_OPTION ),
esc_attr( $name ),
checked( in_array( $name, (array) $settings['post_types'], true ), true, false ),
esc_html( $label )
);
}
echo ' ';
echo '';
echo esc_html__( 'You can turn LiveRe off for a single item in its editor, in the LiveRe Comments box.', 'livere-comments' );
echo '
';
},
'livere-comments',
'livere_comments_main'
);
}
);
add_action(
'admin_menu',
function () {
add_options_page(
__( 'LiveRe Comments', 'livere-comments' ),
__( 'LiveRe Comments', 'livere-comments' ),
'manage_options',
'livere-comments',
'livere_comments_render_settings_page'
);
}
);
/**
* Settings > LiveRe Comments.
*/
function livere_comments_render_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$host = wp_parse_url( home_url(), PHP_URL_HOST );
?>
' . esc_html( (string) $host ) . ''
);
?>
%s',
esc_url( admin_url( 'options-general.php?page=livere-comments' ) ),
esc_html__( 'Settings', 'livere-comments' )
)
);
return $links;
}
);
add_action(
'admin_notices',
function () {
$screen = get_current_screen();
if ( ! $screen || 'plugins' !== $screen->id || ! current_user_can( 'manage_options' ) ) {
return;
}
if ( '' !== livere_comments_get_settings()['client_id'] ) {
return;
}
printf(
'',
esc_html__( 'LiveRe Comments needs your client ID before it can show comments.', 'livere-comments' ),
esc_url( admin_url( 'options-general.php?page=livere-comments' ) ),
esc_html__( 'Add it now', 'livere-comments' )
);
}
);
add_action(
'add_meta_boxes',
function () {
$settings = livere_comments_get_settings();
foreach ( (array) $settings['post_types'] as $post_type ) {
add_meta_box(
'livere-comments',
__( 'LiveRe Comments', 'livere-comments' ),
'livere_comments_render_meta_box',
$post_type,
'side'
);
}
}
);
/**
* The per-post switch.
*
* @param WP_Post $post Post being edited.
*/
function livere_comments_render_meta_box( $post ) {
wp_nonce_field( 'livere_comments_meta_box', 'livere_comments_meta_box_nonce' );
$disabled = '1' === get_post_meta( $post->ID, LIVERE_COMMENTS_DISABLED_META, true );
printf(
' %s ',
checked( ! $disabled, true, false ),
esc_html__( 'Show LiveRe comments', 'livere-comments' )
);
}
add_action(
'save_post',
function ( $post_id ) {
if ( ! isset( $_POST['livere_comments_meta_box_nonce'] )
|| ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['livere_comments_meta_box_nonce'] ) ), 'livere_comments_meta_box' ) ) {
return;
}
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['livere_comments_enabled'] ) ) {
delete_post_meta( $post_id, LIVERE_COMMENTS_DISABLED_META );
} else {
update_post_meta( $post_id, LIVERE_COMMENTS_DISABLED_META, '1' );
}
}
);