Skip to content

Instantly share code, notes, and snippets.

@cesjam7
Created July 2, 2020 20:16
Show Gist options
  • Save cesjam7/31f266b5f134b6e2141516d7bb9b35fa to your computer and use it in GitHub Desktop.
Save cesjam7/31f266b5f134b6e2141516d7bb9b35fa to your computer and use it in GitHub Desktop.
<?php add_action('admin_head', 'c7_admin_head');
function c7_admin_head() { ?>
<style>
.select-autores {
width: 100%;
}
#asignar-autores-modal {
position: fixed;
width: 300px;
min-height: 300px;
z-index: 9999999;
background: #fff;
left: 50%;
margin-left: -150px;
margin-top: -150px;
top: 50%;
border: 1px solid #ddd;
padding: 20px 20px 40px;
box-shadow: 0px 2px 13px rgba(0, 0, 0, 0.4);
}
</style>
<?php }
add_action('admin_footer', 'c7_admin_footer');
function c7_admin_footer() { ?>
<div id="asignar-autores-modal" style="display:none;">
<form method="post">
<h2>Asignar autores a múltiples cursos</h2>
<p>Cursos seleccionados</p>
<p id="cursos-seleccionados"></p>
<p>Seleccionar los autores</p>
<p>
<?php $lista_autores;
$args = array(
'role__in' => array('author', 'administrator'),
);
$autores = get_users( $args );
if ($autores) {
foreach ( $autores as $user ) { ?>
<input type="checkbox" name="autores[]" value="<?php echo $user->user_login; ?>" /><?php echo $user->first_name.' '.$user->last_name.' ('.$user->user_login.')'; ?><br>
<?php }
} ?>
</p>
<p>
<input type="hidden" id="input-cursos" name="cursos">
<input type="submit" name="submit" id="submit" class="button button-primary" value="Conectar autores">
<button id="cancelar-asignacion" class="button button-danger">Cancelar</button>
</p>
</form>
</div>
<script>
jQuery(function($) {
$('.post-type-sfwd-courses .ld-tab-buttons .edit-post-header__settings').before('<a id="asignar-autores" href="#" class="is-button button components-button thickbox">Asignar autores seleccionados</a>')
$('.post-type-sfwd-lessons .ld-tab-buttons .edit-post-header__settings').before('<a id="asignar-autores" href="#" class="is-button button components-button thickbox">Asignar autores seleccionados</a>')
$('.post-type-sfwd-topic .ld-tab-buttons .edit-post-header__settings').before('<a id="asignar-autores" href="#" class="is-button button components-button thickbox">Asignar autores seleccionados</a>')
$('body').on('click', '#asignar-autores', function(e) {
e.preventDefault()
if ($('.wp-list-table .check-column input:checked').length > 0) {
var output = '';
var ids_cursos = []
var c = 0;
$('.wp-list-table .check-column input:checked').each(function() {
var id = $(this).val();
if (id > 0) {
c++;
let curso = $('#post-'+id+' .row-title').text()
output += c+'.- '+curso+'<br>';
ids_cursos.push(id)
}
})
$('#cursos-seleccionados').html(output)
$('#input-cursos').val(ids_cursos.join(','))
$('#asignar-autores-modal').show()
} else {
alert('Debes seleccionar mínimo un post')
}
})
$('body').on('click', '#cancelar-asignacion', function(e) {
e.preventDefault()
$('#asignar-autores-modal').hide()
})
var select_cursos = '<div class="alignleft actions">' +
'<label class="screen-reader-text" for="filter-by-comment-type">Filtrar por curso</label>' +
'<select id="filter-by-comment-type" name="curso">' +
'<option value="">Todos los cursos</option>';
<?php $current = wp_get_current_user();
$cursos = new WP_Query(array(
'post_type' => 'sfwd-courses',
'posts_per_page' => -1,
//'author' => 'cap-'.$current->user_login
));
if ($cursos->have_posts()) {
while ($cursos->have_posts()) { $cursos->the_post();
$author = wp_get_post_terms(get_the_id(), 'author');
$autores = wp_list_pluck($author, 'name');
if (in_array($current->user_login, $autores)) { ?>
select_cursos += '<option value="<?php the_ID(); ?>" <?php if(!empty($_GET['p']) && $_GET['p']==get_the_id()) echo 'selected'; ?>><?php the_title(); ?></option>';
<?php }
}
wp_reset_postdata();
} ?>
select_cursos += '</select>' +
'<input type="submit" name="filter_action" id="post-query-submit" class="button" value="Filtrar"></div>';
$('.edit-comments-php #filter-by-comment-type').parent().before(select_cursos)
})
</script>
<?php }
add_action('admin_init', 'c7_admin_init');
function c7_admin_init() {
if ($_POST && $_POST['submit']=='Conectar autores') {
// print_r($_POST);die;
$cursos = explode(',', $_POST['cursos']);
if ($cursos) {
foreach ($cursos as $idcurso) {
wp_update_post(array(
'ID' => $idcurso,
'post_author' => $_POST['autores'][0]
));
wp_set_post_terms($idcurso, $_POST['autores'], 'author', false);
}
clean_post_cache($idcurso);
}
}
}
add_filter('the_comments', 'wp_filter_comments2');
function wp_filter_comments2($comments){
global $pagenow;
$filtrados = array();
if($pagenow == 'edit-comments.php' && !empty($_GET['curso'])){
$temas = new WP_Query(array(
'post_type' => 'sfwd-lessons',
'posts_per_page' => -1,
'meta_key' => 'course_id',
'meta_value' => $_GET['curso']
));
while ($temas->have_posts()) { $temas->the_post();
array_push($filtrados, get_the_id());
} wp_reset_postdata();
$temas = new WP_Query(array(
'post_type' => 'sfwd-topic',
'posts_per_page' => -1,
'meta_key' => 'course_id',
'meta_value' => $_GET['curso']
));
while ($temas->have_posts()) { $temas->the_post();
array_push($filtrados, get_the_id());
} wp_reset_postdata();
foreach($comments as $i => $comment){
if(!in_array($comment->comment_post_ID, $filtrados)) {
unset($comments[$i]);
}
}
}
return $comments;
}
add_filter( 'parse_query', 'c7_prefix_parse_filter' );
function c7_prefix_parse_filter($query) {
global $pagenow;
$current_page = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
$current_user = wp_get_current_user();
if ( is_admin() &&
'sfwd-courses' == $current_page &&
'edit.php' == $pagenow &&
in_array( 'author', $current_user->roles )) {
global $wpdb;
$terms = $wpdb->get_col("SELECT r.object_id FROM {$wpdb->prefix}terms t
INNER JOIN {$wpdb->prefix}term_relationships r ON r.term_taxonomy_id = t.term_id
WHERE t.name = '".$current_user->user_login."'");
$query->query_vars['post__in'] = $terms;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment