Skip to content

Instantly share code, notes, and snippets.

@racmanuel
Last active August 13, 2024 21:34
Show Gist options
  • Save racmanuel/acc23edb587728f4c1f1ea428b015107 to your computer and use it in GitHub Desktop.
Save racmanuel/acc23edb587728f4c1f1ea428b015107 to your computer and use it in GitHub Desktop.
Custom Birthday Plugin
<?php
/*
Plugin Name: Custom Birthday Plugin
Plugin URI:
Description: Plugin que crea una tabla de cumpleaños en la base de datos.
Version:
Author:
Author URI:
License:
License URI:
*/
// Función para crear la tabla en la activación del plugin
function custom_birthday_create_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_birthday';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id INT NOT NULL AUTO_INCREMENT,
nombre VARCHAR(255) NOT NULL,
correo VARCHAR(255) NOT NULL,
fecha_cumple DATE NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// Hook para ejecutar la función en la activación del plugin
register_activation_hook( __FILE__, 'custom_birthday_create_table' );
function custom_birthday_display_import_form() {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_birthday'; // Asume que así es como has nombrado tu tabla
echo "La fecha y hora actual del servidor es: ";
echo date("Y-m-d H:i:s");
?>
<div class="wrap">
<h1>Importar Cumpleaños</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="csv_file" accept=".csv">
<input type="submit" name="custom_birthday_import" value="Importar">
</form>
<h2>Enviar Felicitaciones</h2>
<form method="post">
<input type="submit" name="custom_birthday_send_greetings" value="Enviar Felicitaciones">
</form>
<h2>Eliminar Todos los Registros</h2>
<form method="post">
<input type="submit" name="custom_birthday_delete_all" value="Eliminar Todos">
</form>
<h2>Cumpleaños del Día de Hoy y Próximos 7 Días</h2>
<table>
<thead>
<tr>
<th>Nombre</th>
<th>Fecha de Cumpleaños</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT nombre, fecha_cumple FROM $table_name WHERE fecha_cumple BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)";
$results = $wpdb->get_results($query);
foreach ($results as $row) {
echo "<tr>";
echo "<td>" . esc_html($row->nombre) . "</td>";
echo "<td>" . esc_html($row->fecha_cumple) . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
<?php
}
if (isset($_POST['custom_birthday_delete_all'])) {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_birthday';
$wpdb->query("TRUNCATE TABLE $table_name");
}
// Hook para mostrar el formulario en una página del menú
add_action( 'admin_menu', 'custom_birthday_add_import_page' );
function custom_birthday_add_import_page() {
add_menu_page(
'Cumpleaños', // Título de la página
'Cumpleaños', // Título del menú
'manage_options', // Capacidad requerida
'custom-birthday-import', // ID de la página
'custom_birthday_display_import_form', // Función que muestra el formulario
'dashicons-buddicons-community', // Icono para el menú
25 // Posición en el menú
);
}
// Función para procesar la importación desde CSV
function custom_birthday_import_csv() {
if ( isset( $_POST['custom_birthday_import'] ) ) {
$file = $_FILES['csv_file']['tmp_name'];
if ( ! empty( $file ) ) {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_birthday';
$handle = fopen( $file, 'r' );
if ( $handle !== false ) {
while ( ( $data = fgetcsv( $handle, 1000, ',' ) ) !== false ) {
$nombre = $data[0];
$correo = $data[1];
$fecha_cumple = $data[2];
$wpdb->insert(
$table_name,
array(
'nombre' => $nombre,
'correo' => $correo,
'fecha_cumple' => $fecha_cumple,
)
);
}
fclose( $handle );
echo 'Datos importados exitosamente.';
} else {
echo 'Error al abrir el archivo CSV.';
}
}
}
}
// Hook para procesar la importación cuando se envíe el formulario
add_action( 'admin_init', 'custom_birthday_import_csv' );
function custom_birthday_send_greetings() {
if ( isset( $_POST['custom_birthday_send_greetings'] ) ) {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_birthday';
$current_date = current_time( 'mysql' );
$current_day = date( 'd', strtotime( $current_date ) );
$current_month = date( 'm', strtotime( $current_date ) );
$fechaCumpleanos = date("d/m/Y");
$results = $wpdb->get_results( "SELECT nombre, correo FROM $table_name WHERE DAY(fecha_cumple) = $current_day AND MONTH(fecha_cumple) = $current_month" );
// Agrega las direcciones de correo para la copia (CC)
$cc_emails = array(
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
);
foreach ( $results as $result ) {
$nombre = $result->nombre;
$correo = $result->correo;
$subject = '¡Feliz Cumpleaños!';
// Diseño personalizado del correo con estilos en línea
$message = '
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<title>
</title>
<!--[if !mso]><!-->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
#outlook a { padding:0; }
body { margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; }
table, td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }
img { border:0;height:auto;line-height:100%; outline:none;text-decoration:none;-ms-interpolation-mode:bicubic; }
p { display:block;margin:13px 0; }
</style>
<!--[if mso]>
<noscript>
<xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
<o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings>
</xml>
</noscript>
<![endif]-->
<!--[if lte mso 11]>
<style type="text/css">
.mj-outlook-group-fix { width:100% !important; }
</style>
<![endif]-->
<!--[if !mso]><!-->
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css">
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);
</style>
<!--<![endif]-->
<style type="text/css">
@media only screen and (min-width:480px) {
.mj-column-px-600 { width:600px !important; max-width: 600px; }
.mj-column-px-400 { width:400px !important; max-width: 400px; }
.mj-column-px-100 { width:100px !important; max-width: 100px; }
}
</style>
<style media="screen and (min-width:480px)">
.moz-text-html .mj-column-px-600 { width:600px !important; max-width: 600px; }
.moz-text-html .mj-column-px-400 { width:400px !important; max-width: 400px; }
.moz-text-html .mj-column-px-100 { width:100px !important; max-width: 100px; }
</style>
<style type="text/css">
@media only screen and (max-width:480px) {
table.mj-full-width-mobile { width: 100% !important; }
td.mj-full-width-mobile { width: auto !important; }
}
</style>
<style type="text/css">
</style>
</head>
<body style="word-spacing:normal;background-color:#eeeeee;">
<div
style="background-color:#eeeeee;"
>
<!-- Image Header -->
<!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation" style="width:600px;" width="600" bgcolor="#000000" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="background:#000000;background-color:#000000;margin:0px auto;max-width:600px;">
<table
align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#000000;background-color:#000000;width:100%;"
>
<tbody>
<tr>
<td
style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"
>
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]-->
<div
class="mj-column-px-600 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"
>
<tbody>
<tr>
<td
align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;"
>
<tbody>
<tr>
<td style="width:300px;">
<img
height="auto" src="https://windsorciclismo.com.mx/wp-content/uploads/2023/07/Windsor-Ciclismo-Logo.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px;" width="300"
/>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation" style="width:600px;" width="600" bgcolor="#ffffff" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="background:#ffffff;background-color:#ffffff;margin:0px auto;max-width:600px;">
<table
align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;"
>
<tbody>
<tr>
<td
style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"
>
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:400px;" ><![endif]-->
<div
class="mj-column-px-400 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"
>
<tbody>
<tr>
<td
align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"
>
<div
style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:24px;line-height:1;text-align:center;color:#000000;"
>¡Feliz Cumpleaños! '.$nombre.'</div>
</td>
</tr>
<tr>
<td
align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"
>
<div
style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:18px;line-height:1;text-align:center;color:#000000;"
>'.$fechaCumpleanos.'</div>
</td>
</tr>
<tr>
<td
align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;"
>
<tbody>
<tr>
<td style="width:350px;">
<img
height="auto" src="https://windsorciclismo.com.mx/wp-content/uploads/2023/08/CUMPLE-WINDSOR-CICLISMO_Mesa-de-trabajo-1-scaled.jpg" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px;" width="350"
/>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
<!-- Icons -->
<!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation" style="width:600px;" width="600" bgcolor="#000000" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="background:#000000;background-color:#000000;margin:0px auto;max-width:600px;">
<table
align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#000000;background-color:#000000;width:100%;"
>
<tbody>
<tr>
<td
style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"
>
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:100px;" ><![endif]-->
<div
class="mj-column-px-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"
>
<tbody>
<tr>
<td
align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;"
>
<tbody>
<tr>
<td style="width:32px;">
<a
href="https://www.facebook.com/WindsorCiclismo/" target="_blank"
>
<img
height="auto" src="https://img.icons8.com/fluency/48/facebook-new.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px;" width="32"
/>
</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td><td class="" style="vertical-align:top;width:100px;" ><![endif]-->
<div
class="mj-column-px-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"
>
<tbody>
<tr>
<td
align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;"
>
<tbody>
<tr>
<td style="width:32px;">
<a
href="https://www.instagram.com/windsorciclismo/" target="_blank"
>
<img
height="auto" src="https://img.icons8.com/fluency/48/instagram-new.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px;" width="32"
/>
</a>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td><td class="" style="vertical-align:top;width:100px;" ><![endif]-->
<div
class="mj-column-px-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"
>
<tbody>
<tr>
<td
align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"
>
<table
border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px;"
>
<tbody>
<tr>
<td style="width:32px;">
<img
height="auto" src="https://img.icons8.com/fluency/48/whatsapp.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px;" width="32"
/>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation" style="width:600px;" width="600" bgcolor="#000000" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="background:#000000;background-color:#000000;margin:0px auto;max-width:600px;">
<table
align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#000000;background-color:#000000;width:100%;"
>
<tbody>
<tr>
<td
style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"
>
<!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr></tr></table><![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]></td></tr></table><![endif]-->
</div>
</body>
</html>
';
$headers = array(
'Content-Type: text/html; charset=UTF-8',
// Agrega las direcciones de correo para la copia (CC)
//'Cc: ' . implode(',', $cc_emails)
);
wp_mail( $correo, $subject, $message, $headers );
}
echo 'Felicitaciones enviadas.';
}
}
// Hook para procesar el envío de felicitaciones
add_action( 'admin_init', 'custom_birthday_send_greetings' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment