Skip to content

Instantly share code, notes, and snippets.

View angelorocha's full-sized avatar
🏠
Working from home

Angelo Rocha angelorocha

🏠
Working from home
View GitHub Profile
@angelorocha
angelorocha / wordpress_sanitize_upload_filename.php
Last active December 6, 2018 18:00
Sanitize wordpress uploaded filenames
<?php
/**
* Include in your theme functions
*/
add_filter( 'sanitize_file_name', 'sanitize_uploaded_filename' );
function sanitize_uploaded_filename( $filename ) {
$fileNewName = sanitize_title( pathinfo( $filename )['filename'] ) . '.' . pathinfo( $filename )['extension'];
return $fileNewName;
}
@angelorocha
angelorocha / get_dir_size.php
Last active June 11, 2021 19:01
Get dir size
<?php
/**
* Usage:
* $get_dir_size("path/to/my/folder");
*/
$get_dir_size = function ($directory){
$size = 0;
$scan_dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach($scan_dir as $file):
@angelorocha
angelorocha / usd_to_brl.php
Last active June 11, 2021 19:14
WordPress - Convert brazilian real in dollar
<?php
/**
* USD to BRL
* API Docs: https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/aplicacao#!/recursos
* https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/aplicacao#!/recursos/CotacaoDolarDia#eyJmb3JtdWxhcmlvIjp7IiRmb3JtYXQiOiJqc29uIiwiJHRvcCI6MTAwLCJkYXRhQ290YWNhbyI6IjA2LTA4LTIwMjEifSwicHJvcHJpZWRhZGVzIjpbMCwxLDJdLCJwZXNxdWlzYWRvIjp0cnVlLCJhY3RpdmVUYWIiOiJkYWRvcyIsImdyaWRTdGF0ZSI6ewMwAzpbewNCAyIEMAQiLANBA30sewNCAyIEMQQiLANBA30sewNCAyIEMgQiLANBA31dLAMxAzp7fSwDMgM6W10sAzMDOnt9LAM0Azp7fSwDNQM6e319LCJwaXZvdE9wdGlvbnMiOnsDYQM6e30sA2IDOltdLANjAzo1MDAsA2QDOltdLANlAzpbXSwDZgM6W10sA2cDOiJrZXlfYV90b196IiwDaAM6ImtleV9hX3RvX3oiLANpAzp7fSwDagM6e30sA2sDOjg1LANsAzpmYWxzZSwDbQM6e30sA24DOnt9LANvAzoiQ29udGFnZW0iLANwAzoiVGFibGUifX0=
* https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)?@dataCotacao=%2706-08-2021%27&$top=100&$format=json&$select=cotacaoCompra,cotacaoVenda,dataHoraCotacao
*
* @param float $value BRL value to convert
* @param string $type '
@angelorocha
angelorocha / post-type-sortable-columns.php
Created June 11, 2021 19:27
WordPress - Make post meta column sortable
<?php
/**
* Usage: cpt_make_sortable_column('post_type', array('coluna'));
*
* @param string $post_type
* @param array $columns
*/
function cpt_make_sortable_column(string $post_type, array $columns = []){
$filter = "manage_edit-{$post_type}_sortable_columns";
@angelorocha
angelorocha / custom-array-multisort.php
Created June 25, 2021 19:12
Order multidimensional array by key
<?php
/**
* Order array by key
* @param $array
* @param $array_key
* @param string $order
*
* @return bool
*/
@angelorocha
angelorocha / csv_to_array.php
Created July 5, 2021 16:53
Convert CSV file in array
<?php
/**
* Convert CSV file in Array
*
* @param string $file_csv CSV file path
*
* @return array|string
*/
function csv_to_array(string $file_csv){
if(!file_exists($file_csv)):
@angelorocha
angelorocha / wpss_get_remote_image.php
Created August 2, 2021 02:58
Get remote images and save as WordPress attachment
<?php
/**
* @author Angelo Rocha
* @copyleft 2021
* @license GNU GPL 3 (https://www.gnu.org/licenses/gpl-3.0.html)
*
* @param string $image Set url to get image
*
*/
@angelorocha
angelorocha / redux_sample.php
Created August 24, 2021 12:57
Problem with assets dir
<?php
if( !class_exists('Redux') ) {
return;
}
$opt_name = 'wpss_option';
$theme = wp_get_theme();
@angelorocha
angelorocha / WPSSCtp.php
Last active October 22, 2021 20:13
WordPress class to generate post types
<?php
/**
* Class WPSSCpt
* Generate custom post types
*/
class WPSSCpt {
/**
* Class instance
@angelorocha
angelorocha / WPSSDummy.php
Last active December 20, 2021 13:24
Generate WordPress dummy posts with content and post thumbnail
<?php
/**
* Class WPSSDummy
*
* Generate dummy posts, see parameters of loripsum api:
*
* (integer) - The number of paragraphs to generate.
* short, medium, long, verylong - The average length of a paragraph.
* decorate - Add bold, italic and marked text.