Skip to content

Instantly share code, notes, and snippets.

@DavidPeralvarez
Created March 29, 2019 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidPeralvarez/be6de691d91eb3381d8eeba6a362c285 to your computer and use it in GitHub Desktop.
Save DavidPeralvarez/be6de691d91eb3381d8eeba6a362c285 to your computer and use it in GitHub Desktop.
Comprobar los permisos del usuario
<?php
/*
Plugin Name: Seguridad en Plugins
Description: Aprendiendo a hacer nuestros plugins más seguros
Text Domain: seguridad-plugins
Domain Path: /languages
*/
/**
* Añadimos un enlace debajo del contenido de un post para poder eliminarlo
*/
add_filter( 'the_content', 'dp_sep_add_delete_link' );
function dp_sep_add_delete_link( $content ){
$new_content = $content;
if( is_single() && in_the_loop() && is_main_query() && current_user_can('edit_others_posts') ):
// http://cursodesarrolloplugins.local/?action=dp_sep_delete&post_id=8
$url = add_query_arg( array(
'action' => 'dp_sep_delete',
'post_id' => get_the_ID()
), home_url() );
$new_content .= '<a href="'.$url.'">'.__( 'Eliminar post', 'seguridad-plugins').'</a>';
endif;
return $new_content;
}
/**
* Recuperamos los parámetros de la URL y eliminamos el post
*/
add_action( 'init', 'dp_sep_delete_post' );
function dp_sep_delete_post(){
if( isset( $_GET['action'] ) && isset( $_GET['post_id'] ) && current_user_can('edit_others_posts') ):
// Enviamos a la papelera este post
wp_trash_post( $_GET['post_id'] );
// Redireccionamos a la home
wp_safe_redirect( home_url() );
exit;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment