Skip to content

Instantly share code, notes, and snippets.

View andrija-naglic's full-sized avatar

Andrija andrija-naglic

View GitHub Profile
@andrija-naglic
andrija-naglic / vessi.md
Last active May 27, 2023 02:03
Custom plugin for Vessi website
@andrija-naglic
andrija-naglic / mepr-active-memberships.php
Created September 5, 2018 22:30
Get a list of the current user's active MemberPress Subscriptions
<?php
function memberpress__get_all_memberships( $user_id = false ){
if( class_exists('MeprUser') ){
if( ! $user_id ){
$user_id = get_current_user_id();
}
@andrija-naglic
andrija-naglic / example.php
Created September 3, 2018 20:43
WP MemberPress - get list of memberships that can access a restricted content
<?php
$the_post_object = get_post( $post_id );
$access_list = MeprRule::get_access_list( $the_post_object );
$product_ids = (isset($access_list['membership']) && !empty($access_list['membership'])) ? $access_list['membership'] : array();
/*
$acces_list returns: ["membership" => [1,2], "member" =>["foo"]]
$products_ids contains an array of Membership IDs that can access this content
@andrija-naglic
andrija-naglic / write_log.php
Created September 3, 2018 17:52
Short WP function that is used to output custom data to debug.log
<?php
function write_log ( $log, $text = "write_log: ", $delete = false ) {
if( $delete ){
unlink( WP_CONTENT_DIR . '/debug.log' );
}
if ( is_array( $log ) || is_object( $log ) ) {
error_log( $text . PHP_EOL . print_r( $log, true ) );
@andrija-naglic
andrija-naglic / enqueue.php
Last active August 26, 2018 14:22
My way to enqueue scripts in WordPress and load from cache only if the file is not changed
<?php
// In plugins
// ----------
$and_js_ver = date("ymd-Gis", filemtime( plugin_dir_path( __FILE__ ) . 'js/main.js' ));
wp_enqueue_script( 'and_js', plugins_url( 'js/main.js', __FILE__ ), array(), $and_js_ver );
$and_css_ver = date("ymd-Gis", filemtime( plugin_dir_path( __FILE__ ) . 'style.css' ));
wp_register_style( 'and_css', plugins_url( 'style.css', __FILE__ ), array(), $and_css_ver );
@andrija-naglic
andrija-naglic / bayesian.sql
Last active August 14, 2016 20:03
The Bayesian estimate formula useful for sorting the ratings properly
/*
The problem is described here: http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
And the formula is from IMDB: http://stackoverflow.com/a/1411268/5108832
It's a Bayesian estimate.
This is the formula that is used to sort the ratings:
( (avg_num_votes * avg_rating) + (this_num_votes * this_rating) ) / (avg_num_votes + this_num_votes)
In this example, the sample data are 9 posts (much like the WordPress posts) with a 5 star ratings.