Skip to content

Instantly share code, notes, and snippets.

View krasenslavov's full-sized avatar
Work in progress

Krasen Slavov krasenslavov

Work in progress
View GitHub Profile
@krasenslavov
krasenslavov / woo-categories-arr.php
Last active February 26, 2019 09:02
WooCommerce product categories hierarchy (in array).
<?php
function get_woocats_arr( $parent = 0, $hierarchy = array() ) {
$categories = get_terms(
array(
'taxonomy' => 'product_cat',
'orderby' => 'count',
'child_of' => $parent,
)
@krasenslavov
krasenslavov / api_license_key_server.php
Last active March 18, 2022 16:04
API server for our license key feature. Visit blog posts https://bit.ly/2Vwobsu
<?php
// Load up WordPress.
require_once '../wp-load.php';
// Include license key funcitons that handle the options.
require_once 'options.php';
// Include license key controller functions that handle the routes.
require_once 'controllers.php';
@krasenslavov
krasenslavov / api_license_key_server_controllers.php
Last active March 18, 2022 16:06
Controllers for our license key API server. Visit blog post https://bit.ly/2Vwobsu
<?php
// 1. Return license key & sites after successful authentication.
function get_license($req, $res, $args) {
$params = $req->getParams();
$header = $req->getHeaders();
$license_key = get_license_key($header['PHP_AUTH_USER'][0]);
@krasenslavov
krasenslavov / dashboard_plugin_tracker_widget.php
Last active April 26, 2020 15:31
WordPress.org plugin tracker dashboard widget. Visit blog post https://bit.ly/2xy3dSj
<?php
add_action('admin_enqueue_scripts', function() {
wp_add_inline_style('dashboard', '
.plugin-box-container {
display: flex;
flex-wrap: wrap;
max-height: 288px;
overflow-y: auto;
@krasenslavov
krasenslavov / api_license_key_server_options.php
Last active April 26, 2020 15:29
API license key functions/options. Visit blog posts https://bit.ly/2XziqNy
<?php
/* 1. Generate initial license key data. */
function generate_license_key(string $user_name, string $password, int $total_installs = 1) {
if (!strstr($user_name, '_license_key')) {
$user_name .= '_license_key';
}
// Shouldn't be able to continue if there is license key found.
@krasenslavov
krasenslavov / send_email_functions.php
Last active April 26, 2020 15:33
Sending custom emails in WordPress. Visit blog post https://bit.ly/2xlZG9L
<?php
/**
$email_custom_data = array(
'title' => '',
'content' => '',
'logo_url' => '',
'featured_image_url' => '',
'cta_button_url' => '',
'cta_button_text' => '',
@krasenslavov
krasenslavov / send_email_template.php
Last active March 18, 2022 16:16
Sending custom emails in WordPress. Visit blog post https://bit.ly/2xlZG9L
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?php echo $email_custom_data['title'];?> - Company Name</title>
<style>
@font-face {
font-family: 'Roboto';
font-style: normal;
@krasenslavov
krasenslavov / send_email_actions.php
Last active April 26, 2020 15:33
Sending custom emails in WordPress. Visit blog post https://bit.ly/2xlZG9L
<?php
add_action('wp_mail_content_type', function() {
return "text/html";
}, 10, 1);
add_action('retrieve_password_message', function($message, $reset_key, $user_login, $user_data) {
$logo_url = esc_url('https://cdn-domain-name.com/assets/img/logo.png');
@krasenslavov
krasenslavov / api_license_key_plugin.php
Last active March 18, 2022 16:11
API license key plugin verification. Visit blog post https://bit.ly/2xlZKGx
<?php
require_once 'vendor/autoload.php';
$http_client = new \GuzzleHttp\Client();
$base_url = 'https://api.example.com/';
$error_message = '';
$success_message = '';
$logged_in = false;
@krasenslavov
krasenslavov / api_license_key_plugin_form.php
Last active March 18, 2022 16:08
API license key plugin form. Visit blog post https://bit.ly/2xlZKGx
<h1>License Key</h1>
<?php if ($error_message):?>
<div class="notice notice-error is-dismissible">
<p><strong><?php echo $error_message;?></strong></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
</div>
<?php endif; ?>