Skip to content

Instantly share code, notes, and snippets.

View certainlyakey's full-sized avatar

Aleksandr Beliaev certainlyakey

  • Nortal AS
  • Tallinn
View GitHub Profile
@certainlyakey
certainlyakey / functions.php
Last active May 15, 2022 20:40
Integrate Google Recaptcha v2 into custom AJAX form (Wordpress)
<?php
// First we register the Google Recaptcha script as a dependency with an onload callback
wp_register_script( 'recaptcha_v2', add_query_arg(['render' => 'explicit', 'onload' => 'onloadRecaptchaCallback'], 'https://www.google.com/recaptcha/api.js'), [], null, true);
wp_enqueue_script( 'recaptcha_v2' );
// Here we validate Google Recaptcha token issued on our frontend with Google servers using our custom 'validate_captcha' WP AJAX hook
function hh_validate_captcha() {
check_ajax_referer( 'hhsubscribe', '_ajax_nonce' );
@certainlyakey
certainlyakey / enable-trigger-browsersync.php
Created August 15, 2019 17:46
Activate Browsersync reload on admin actions Wordpress plugin (requires Trigger Browsersync plugin)
<?php
/*
Plugin Name: Activate Browsersync
Plugin URI: https://wordpress.org/plugins/trigger-browsersync/
Description: Activates Browsersync. Doesn't do anything without <a href="https://wordpress.org/plugins/trigger-browsersync/">Trigger Browsersync</a> plugin. This plugin code should be only present in local environment.
Version: 0.0.1
Author: Sami Greenbury
Author URI: https://www.patabugen.co.uk/
*/
@certainlyakey
certainlyakey / _mixins.scss
Last active June 11, 2019 14:52
A mixin that applies responsive spacing from a map
// A mixin that applies responsive spacing from a map
// Usage: @include u-set-spacing-style('page-spacing');
@mixin u-set-spacing-style($group-name) {
$group: map-get($spacing, $group-name);
@each $property, $value in $group {
$is-breakpoint: variable-exists('breakpoints') and map-has-key($breakpoints, $property) and type-of($value) == 'map';
@include u-set-breakpoint(if($is-breakpoint, $property, null)) {
@if ($is-breakpoint) {
@certainlyakey
certainlyakey / _colors.scss
Last active June 11, 2019 11:11
A mixin that applies colors from a map
$colors-by-name: (
dove-gray: #666666,
mine-shaft: #333333,
denim: #0f6bac,
niagara: #0fb3ac,
// ...
);
$colors-by-function: (
buttons: (
@certainlyakey
certainlyakey / _mixins.scss
Last active June 12, 2019 10:21
A mixin that applies typographic styles from a map
// A mixin that allows to apply typographic styles from a map
// Supports applying a typographic style responsively, based on a map of media queries in a $breakpoints variable
@mixin u-set-typography($typeface-name: 'body', $style-name: 'regular') {
$this-typeface: map-get($font-styles, $typeface-name);
$this-style: map-get($this-typeface, $style-name);
@if $typeface-name == 'body' {
font-family: $font-body;
}
@certainlyakey
certainlyakey / component-a.scss
Created December 19, 2018 09:10
Component mixin example
@mixin c-component-a($base_class: &) {
// styling for the root tag
color:gray;
// styling for elements and modifiers
// it is contained within @at-root directive to lower specificity
@at-root {
#{$base}__element-a {
width:400px;
@certainlyakey
certainlyakey / positioning.ts
Last active August 24, 2018 10:36
positionElements utility from ng-bootstrap 3.0.0
// from ng-bootstrap. Is not publicly accessible starting from 3.0.0 unfortunately so we have to copy it (also is changed slighly)
export class Positioning {
private getAllStyles(element: HTMLElement) { return window.getComputedStyle(element); }
private getStyle(element: HTMLElement, prop: string): string { return this.getAllStyles(element)[prop]; }
private isStaticPositioned(element: HTMLElement): boolean {
return (this.getStyle(element, 'position') || 'static') === 'static';
}
@certainlyakey
certainlyakey / _utilities.scss
Last active August 2, 2018 12:37
Last parent selector in SASS (like & but only the last one)
// same as &, but applies the last (closest) parent only
// usage: @include u-direct-parent('.parent'<, ...>) {}
@mixin u-direct-parent($parent-selectors...) {
@each $parent-selector in $parent-selectors {
$current-sequences: &;
$new-sequences: ();
@each $sequence in $current-sequences {
$current-selector: nth($sequence, -1);
$prepended-selector: join($parent-selector, $current-selector);
@certainlyakey
certainlyakey / functions.php
Created March 19, 2018 08:16
Gravity Forms + Wordpress — retrieve a Gravity form by its class (set in each form settings)
<?php
// Retrieve a Gravity form by class (set in each form settings) 'js-overlay-form'
if ( class_exists( 'GFAPI' ) ) {
$forms = GFAPI::get_forms();
$class_key = 'cssClass';
$contactform_class = 'js-overlay-form';
foreach ($forms as $form) {
if (array_key_exists($class_key, $form) && $form[$class_key] == $contactform_class) {
$contactform_id = $form['id'];
break;
@certainlyakey
certainlyakey / functions.php
Created March 16, 2018 10:11
Remove current post from relationship fields which refer to the same post type (Wordpress + ACF)
<?php
// Remove current post from relationship fields which refer to the same post type
function remove_current_post_from_self_relationships($args, $field, $post_id) {
$args['post__not_in'] = array($post_id);
return $args;
}
$self_relationship_fields = array('relation_cases_cases');
foreach ($self_relationship_fields as $key => $field_name) {