primeiro commit
Signed-off-by: Rodrigo Bertoldo de Sousa Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
commit
e6a6f9c159
19 files changed
+3202
No files matched your search
+134
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* Functions and definitions
|
||||
*
|
||||
* @package MartinsEVieira
|
||||
*/
|
||||
|
||||
// Constantes de versão para cache busting
|
||||
define('THEME_VERSION', '1.0.1');
|
||||
|
||||
/**
|
||||
* Theme setup
|
||||
*/
|
||||
function martinsevieira_setup() {
|
||||
add_theme_support('title-tag');
|
||||
add_theme_support('post-thumbnails');
|
||||
|
||||
register_nav_menus(array(
|
||||
'primary' => 'Menu Principal',
|
||||
));
|
||||
|
||||
add_theme_support('html5', array(
|
||||
'search-form',
|
||||
'comment-form',
|
||||
'comment-list',
|
||||
'gallery',
|
||||
'caption',
|
||||
));
|
||||
|
||||
// Adicionar tamanhos de imagem
|
||||
add_image_size('post-thumb', 400, 250, true);
|
||||
}
|
||||
add_action('after_setup_theme', 'martinsevieira_setup');
|
||||
|
||||
/**
|
||||
* Enqueue scripts and styles
|
||||
*/
|
||||
function martinsevieira_scripts() {
|
||||
// Styles
|
||||
wp_enqueue_style('martinsevieira-style', get_stylesheet_uri(), array(), THEME_VERSION);
|
||||
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css', array(), '6.4.0');
|
||||
|
||||
// Scripts
|
||||
wp_enqueue_script('martinsevieira-script', get_template_directory_uri() . '/js/main.js', array(), THEME_VERSION, true);
|
||||
|
||||
// Localizar script para AJAX
|
||||
wp_localize_script('martinsevieira-script', 'ajaxData', array(
|
||||
'ajaxurl' => admin_url('admin-ajax.php'),
|
||||
'nonce' => wp_create_nonce('load_more_posts_nonce')
|
||||
));
|
||||
}
|
||||
add_action('wp_enqueue_scripts', 'martinsevieira_scripts');
|
||||
|
||||
/**
|
||||
* Register widget areas
|
||||
*/
|
||||
function martinsevieira_widgets_init() {
|
||||
register_sidebar(array(
|
||||
'name' => 'Sidebar Principal',
|
||||
'id' => 'sidebar-1',
|
||||
'description' => 'Sidebar principal do site',
|
||||
'before_widget' => '<div class="widget">',
|
||||
'after_widget' => '</div>',
|
||||
'before_title' => '<h3 class="widget-title">',
|
||||
'after_title' => '</h3>',
|
||||
));
|
||||
}
|
||||
add_action('widgets_init', 'martinsevieira_widgets_init');
|
||||
|
||||
/**
|
||||
* AJAX - Carregar mais posts
|
||||
*/
|
||||
function load_more_posts_ajax() {
|
||||
check_ajax_referer('load_more_posts_nonce', 'nonce');
|
||||
|
||||
$page = isset($_POST['page']) ? absint($_POST['page']) : 1;
|
||||
$posts_per_page = 3;
|
||||
|
||||
// Cache key único para esta página
|
||||
$cache_key = 'load_more_posts_' . $page;
|
||||
$cached_data = get_transient($cache_key);
|
||||
|
||||
if (false !== $cached_data) {
|
||||
wp_send_json_success($cached_data);
|
||||
return;
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'posts_per_page' => $posts_per_page,
|
||||
'paged' => $page,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
'no_found_rows' => false, // Necessário para max_num_pages
|
||||
);
|
||||
|
||||
$query = new WP_Query($args);
|
||||
|
||||
if ($query->have_posts()) :
|
||||
ob_start();
|
||||
while ($query->have_posts()) : $query->the_post();
|
||||
get_template_part('template-parts/content', 'card');
|
||||
endwhile;
|
||||
$posts_html = ob_get_clean();
|
||||
wp_reset_postdata();
|
||||
|
||||
$response = array(
|
||||
'posts' => $posts_html,
|
||||
'max_pages' => $query->max_num_pages
|
||||
);
|
||||
|
||||
// Cachear por 1 hora
|
||||
set_transient($cache_key, $response, HOUR_IN_SECONDS);
|
||||
|
||||
wp_send_json_success($response);
|
||||
else :
|
||||
wp_send_json_error(array('message' => 'Não há mais posts.'));
|
||||
endif;
|
||||
}
|
||||
add_action('wp_ajax_load_more_posts', 'load_more_posts_ajax');
|
||||
add_action('wp_ajax_nopriv_load_more_posts', 'load_more_posts_ajax');
|
||||
|
||||
/**
|
||||
* Limpar cache ao publicar/atualizar posts
|
||||
*/
|
||||
function clear_posts_cache($post_id) {
|
||||
if (get_post_type($post_id) === 'post') {
|
||||
global $wpdb;
|
||||
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_load_more_posts_%'");
|
||||
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_load_more_posts_%'");
|
||||
}
|
||||
}
|
||||
add_action('save_post', 'clear_posts_cache');
|
||||
add_action('delete_post', 'clear_posts_cache');
|
||||
Reference in new issue
Block a user