Last active
July 12, 2023 18:10
-
-
Save dantetesta/47e1c850238cf2b23835748b79d81bc4 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 | |
| // O WordPress 'add_action' registra uma nova ação com a tag 'jet-engine/register-macros' | |
| add_action( 'jet-engine/register-macros', function() { | |
| /** | |
| * Classe que formata uma string de data. | |
| */ | |
| class Format_Date_String extends \Jet_Engine_Base_Macros { | |
| // Retorna a tag da macro | |
| public function macros_tag() { | |
| return 'format_date_string'; | |
| } | |
| // Retorna o nome da macro | |
| public function macros_name() { | |
| return esc_html__( 'Format date string', 'jet-engine' ); | |
| } | |
| // Define os argumentos que a macro aceita | |
| public function macros_args() { | |
| return array( | |
| 'format' => array( | |
| 'label' => __( 'Format', 'jet-engine' ), // Rótulo para o argumento 'format' | |
| 'type' => 'text', // Tipo do argumento 'format' | |
| 'default' => 'd.m.Y', // Valor padrão para 'format' | |
| ), | |
| 'date_string' => array( | |
| 'label' => __( 'Date string', 'jet-engine' ), // Rótulo para 'date_string' | |
| 'type' => 'text', // Tipo para 'date_string' | |
| 'default' => 'now', // Valor padrão para 'date_string' | |
| ), | |
| 'show_by' => array( | |
| 'label' => 'Show by:', // Rótulo para 'show_by' | |
| 'type' => 'select', // Tipo para 'show_by' | |
| 'options' => array( | |
| 'utc' => 'UTC+0', // Opção 'UTC+0' para 'show_by' | |
| 'server' => 'Server time' // Opção 'Server time' para 'show_by' | |
| ), | |
| 'default' => 'utc', // Valor padrão para 'show_by' | |
| ), | |
| ); | |
| } | |
| // Função de retorno de chamada que é executada quando a macro é chamada | |
| public function macros_callback( $args = array() ) { | |
| // Busca os argumentos | |
| $format = $args['format'] ?? ''; | |
| $date_string = $args['date_string'] ?? ''; | |
| $show_by = $args['show_by'] ?? 'utc'; | |
| // Se 'format' ou 'date_string' não foram fornecidos, retorna 'cannot format' | |
| if ( ! $format || ! $date_string ) { | |
| return 'cannot format'; | |
| } | |
| // Se 'format' for 'datetime', muda para 'Y-m-d\TH:i' | |
| if ( $format === 'datetime' ) { | |
| $format = 'Y-m-d\TH:i'; | |
| } | |
| // Se 'show_by' for 'utc', deixa 'base_timestamp' como null. Caso contrário, calcula a hora do servidor | |
| if ( $show_by === 'utc' ) { | |
| $base_timestamp = null; | |
| } else { | |
| $base_timestamp = strtotime( 'now' ) + get_option( 'gmt_offset' ) * 3600; | |
| } | |
| // Calcula o timestamp para 'date_string' | |
| $timestamp = strtotime( $date_string, $base_timestamp ); | |
| // Formata o timestamp para o formato fornecido em 'format' | |
| $result = date( $format, $timestamp ); | |
| // Retorna a string de data formatada | |
| return $result; | |
| } | |
| } | |
| // Instancia a classe, que registra a macro | |
| new Format_Date_String(); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment