Skip to content

Instantly share code, notes, and snippets.

View YOzaz's full-sized avatar

Marijus Plančiūnas YOzaz

View GitHub Profile
@YOzaz
YOzaz / check2optimized.php
Created December 3, 2018 12:59
Check if image exists - REGEX optimization
<?php
global $wpdb;
$query = 'SELECT `wp_posts`.`ID`, `wp_posts`.`guid`, `wp_postmeta`.`meta_value`
FROM `wp_posts`, `wp_postmeta`
WHERE `post_type` = "attachment"
AND `wp_posts`.`ID` = `wp_postmeta`.`post_id`
AND `wp_postmeta`.`meta_key` = "_wp_attachment_metadata"
AND `wp_postmeta`.`meta_value` REGEXP \'^a:[[:alnum:]]+:{s:5:"width";i:$$$PIXELS_WIDTH$$;s:6:"height";i:$$$PIXELS_HEIGHT$$$;s:4:"file";s:[[:alnum:]]+:"(.*?/|)$$$FILENAME$$$"\'';
$query = str_replace('$$$PIXELS_WIDTH$$$', $width, $query);
$query = str_replace('$$$PIXELS_HEIGHT$$$', $height, $query);
@YOzaz
YOzaz / check2updated.php
Last active December 3, 2018 13:31
Check if image exists and compare file size
<?php
stream_context_set_default( ['http' => ['method' => 'HEAD'] ] );
$head = array_change_key_case( get_headers( $fullremotefilename, 1 ) );
$content_length = isset($head['content-length']) ? (int)( $head['content-length'] ) : 0;
if ( $content_length ) {
$img_size = @filesize( $localfilename );
if ( $img_size !== false && $img_size !== $content_length ) {
// images match!
} else {
// images don't match
@YOzaz
YOzaz / check2.php
Last active December 3, 2018 12:40
Check if image exists using `wp_postmeta` table
<?php
global $wpdb;
$query = 'SELECT `wp_posts`.`ID`, `wp_posts`.`guid`, `wp_postmeta`.`meta_value`
FROM `wp_posts`, `wp_postmeta`
WHERE `post_type` = "attachment"
AND `wp_posts`.`ID` = `wp_postmeta`.`post_id`
AND `wp_postmeta`.`meta_key` = "_wp_attachment_metadata"
AND `wp_postmeta`.`meta_value` LIKE \'a:%:{s:5:"width";i:$$$PIXELS_WIDTH$$$;s:6:"height";i:$$$PIXELS_HEIGHT$$$;s:4:"file";s:%:"%$$$FILENAME$$$"%\'';
$query = str_replace('$$$PIXELS_WIDTH$$$', $width, $query);
$query = str_replace('$$$PIXELS_HEIGHT$$$', $height, $query);
@YOzaz
YOzaz / check.php
Last active December 3, 2018 12:31
Check if image exists using `wp_posts` table
<?php
global $wpdb;
$image_src = wp_upload_dir()['baseurl'] . '/' . _wp_relative_upload_path( $filename );
$query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE guid='$image_src'";
$count = intval($wpdb->get_var($query));
local return_code="%(?..%{$FG[209]%}%? %{$reset_color%})"
PROMPT='%{$FG[110]%}{ %c } \
%{$FG[150]%}$( git rev-parse --abbrev-ref HEAD 2> /dev/null || echo "" )%{$reset_color%} \
%{$FG[209]%}%(!.#.»)%{$reset_color%} '
PROMPT2='%{$FG[209]%}\ %{$reset_color%}'
RPS1='%{$FG[223]%}%n@%m: %{$reset_color%}\
%{$FG[110]%}%~%{$reset_color%} ${return_code}'
<?php namespace MyApp;
use Config;
trait LocalPathsTrait
{
/**
* Replaces remote URL to Local Path
*
* @param string $url remote image or file (with http and www stuff)
<?php
trait PresentersHelper
{
/**
* @param \Carbon\Carbon $date
*
* @return string
*/
public function niceTime( $date = null )
<?php
use Laracasts\Presenter\Presenter;
abstract class AbstractPresenter extends Presenter
{
/**
* @param \Carbon\Carbon $date
*
* @return string
function isTransactionTrackingPresent(symbol)
{
symbol = symbol || 'trackTrans';
var present = false;
var script_tags = document.getElementsByTagName("script");
Array.prototype.slice.call(script_tags).forEach( function(tag)
{
if ( present ) return;
var source = tag.innerText || tag.textContent;
if ( source && source.indexOf(symbol) != -1 )
<?php
/**
* Lazy loads relationship if required
*
* @param \Eloquent $model
* @param string $relationship
*
* @return object
*/