Skip to content

Instantly share code, notes, and snippets.

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

Dm.Korolchuk welch08

🏠
Working from home
View GitHub Profile
@welch08
welch08 / jsonFixer.php
Created December 1, 2022 10:31
Fixer for invalid JSON string (PHP)
<?php
function jsonFixer($json){
$patterns = [];
/** garbage removal */
$patterns[0] = "/([\s:,\{}\[\]])\s*'([^:,\{}\[\]]*)'\s*([\s:,\{}\[\]])/"; //Find any character except colons, commas, curly and square brackets surrounded or not by spaces preceded and followed by spaces, colons, commas, curly or square brackets...
$patterns[1] = '/([^\s:,\{}\[\]]*)\{([^\s:,\{}\[\]]*)/'; //Find any left curly brackets surrounded or not by one or more of any character except spaces, colons, commas, curly and square brackets...
$patterns[2] = "/([^\s:,\{}\[\]]+)}/"; //Find any right curly brackets preceded by one or more of any character except spaces, colons, commas, curly and square brackets...
$patterns[3] = "/(}),\s*/"; //JSON.parse() doesn't allow trailing commas
/** reformatting */
$patterns[4] = '/([^\s:,\{}\[\]]+\s*)*[^\s:,\{}\[\]]+/'; //Find or not one or more of any character except spaces, colons, commas, curly and square brackets followed by one or more of any character except spaces, c
@welch08
welch08 / compose.js
Created February 22, 2022 18:37
JS: Compose functions
// ref: https://1loc.dev/function/compose-functions/
// Compose functions from right to left
const compose =
(...fns) =>
(x) =>
fns.reduceRight((y, f) => f(y), x);
// Example
const lowercase = (str) => str.toLowerCase();
@welch08
welch08 / wordpress_ajax_with_fetch.js
Last active February 19, 2023 14:22
Wordpress Fetch Ajax Request
const sendRequest = async data => {
try {
const response = await fetch(window.ajax_url, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache',
},
body: new URLSearchParams(data)
@welch08
welch08 / gist:3f097a7e4f20cb0fa4de3bc29f67b585
Created December 5, 2019 12:42
Add Admin User via SQL WP
INSERT INTO `wp_users` (`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) VALUES ('11', 'admin', MD5('admin'), 'Admin', 'admin@test.com', '', '2019-11-11 00:00:00', '', '0', 'Admin');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '11', 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}');
INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, '11', 'wp_user_level', '10');
@welch08
welch08 / get_fan_count.php
Created August 5, 2019 14:47
get_fan_count
</php
function get_fan_count(){
$fb_id = '106900272716297';
$count = get_transient('fan_count');
if ($count !== false) return $count;
$count = 0;
$data = wp_remote_get('http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id='.$fb_id.'');
if (is_wp_error($data)) {
@welch08
welch08 / post_views.php
Last active August 14, 2019 15:00
wp set post views
<?php
/**
* Sets post views during create a post
* @param int $post_id The post ID.
* @param object $post The post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
function add_post_views_after_save_post( $post_ID, $post, $update ) {
$count_key = 'post_views_count';
$count = get_post_meta($post_ID, $count_key, true);
@welch08
welch08 / rank.php
Created July 16, 2019 10:54
convert numbers to visual rating
<ul class="stars-rating">
<?php
for($x=1;$x<=$rank;$x++) {
echo "<li class=\"stars-rating_item full\"><i class=\"star\">$x</i></li>";
}
if (strpos($rank,'.')) {
echo "<li class=\"stars-rating_item half\"><i class=\"star\">$x</i></li>";
$x++;
}
while ($x<=5) {
@welch08
welch08 / edge_support.css
Created March 27, 2019 22:01
check edge @support
@supports (-ms-ime-align:auto) {
.selector {
property: value;
}
}
/* https://browserstrangeness.github.io/css_hacks.html */
@welch08
welch08 / wp_genetare_xml_sitemap.php
Created September 12, 2018 21:59
Generate XML sitemap
<?php
/*
function to create sitemap.xml file in root directory of site
*/
add_action("publish_post", "pm_create_sitemap");
add_action("publish_page", "pm_create_sitemap");
add_action( "save_post", "pm_create_sitemap" );
@welch08
welch08 / acf_qt_fix.php
Created August 20, 2018 07:38
acf relationfield qTranslate admin panel fix
<?php
function my_relationship_result( $title, $post, $field, $post_id ) {
$editLang = $_COOKIE['qtrans_edit_language'];
if($editLang) {
return qtranxf_use($editLang, $title, false, false);
} else {
return qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage($title);
}
}