Created
July 1, 2020 18:37
-
-
Save cesjam7/a343902f15b1c8a6c7a3f3c38459def9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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; | |
$autores = get_terms(array( | |
'taxonomy' => 'author', | |
'hide_empty' => false | |
)); | |
if ($autores) { | |
foreach ($autores as $autor) { | |
$user = get_user_by('slug', $autor->name); | |
if ($user && (in_array( 'author', (array) $user->roles ) || in_array( 'administrator', (array) $user->roles )) ) { ?> | |
<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) { | |
echo 'actualziar '.$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment