;. */ class BadgeOS_Achievement_Shortcode { function __construct() { // Define plugin constants $this->basename = plugin_basename( __FILE__ ); $this->directory_path = plugin_dir_path( __FILE__ ); $this->directory_url = plugin_dir_url( __FILE__ ); // Load translations load_plugin_textdomain( 'achievement-shortcode-for-badgeos', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); // If BadgeOS is unavailable, deactivate our plugin add_action( 'admin_notices', array( $this, 'maybe_disable_plugin' ) ); add_action( 'init', array( $this, 'register_badgeos_shortcodes' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 99 ); } public function register_badgeos_shortcodes() { badgeos_register_shortcode( array( 'name' => __( 'User earned achievement', 'achievement-shortcode-for-badgeos' ), 'slug' => 'user_earned_achievement', 'description' => __( 'Show or hide content depending on if the user has earned a specific achievement.', 'achievement-shortcode-for-badgeos' ), 'output_callback' => array( $this, 'shortcode' ), 'attributes' => array( 'id' => array( 'name' => __( 'Achievement ID', 'achievement-shortcode-for-badgeos' ), 'description' => __( 'The ID of the achievement the user must have earned.', 'achievement-shortcode-for-badgeos' ), 'type' => 'text', ), ), ) ); } /** * Enqueue and localize relevant admin_scripts. * * @since 1.0.4 */ public function admin_scripts() { wp_enqueue_script( 'rangyinputs-jquery', $this->directory_url . 'js/rangyinputs-jquery-src.js', array( 'jquery' ), '', true ); wp_enqueue_script( 'activity-progress-shortcode-embed', $this->directory_url . 'js/achievement-shortcode-embed.js', array( 'rangyinputs-jquery', 'badgeos-select2' ), '', true ); } public function shortcode( $atts, $content = null ) { $atts = shortcode_atts( array( 'id' => false, // achievement ), $atts ); $achievement = $atts['id']; $user_id = get_current_user_id(); $user_has_achievement = $user_id && badgeos_has_user_earned_achievement( intval( $achievement ), $user_id ); $return = ''; if ( ! $achievement ) { $return = '
' . __( 'You have to specify a valid achievement id in the "id" parameter!', 'achievement-shortcode-for-badgeos' ) . '
'; } elseif ( $user_has_achievement ) { $return = do_shortcode( $content ); } return $return; } /** * Check if BadgeOS is available * * @since 1.0.0 * @return bool True if BadgeOS is available, false otherwise */ public static function meets_requirements() { if ( class_exists( 'BadgeOS' ) && version_compare( BadgeOS::$version, '1.4.0', '>=' ) ) { return true; } else { return false; } } /** * Generate a custom error message and deactivates the plugin if we don't meet requirements * * @since 1.0.0 */ public function maybe_disable_plugin() { if ( ! $this->meets_requirements() ) { // Display our error echo '
'; echo '

' . sprintf( __( 'BadgeOS Achievement Shortcode Add-On requires BadgeOS 1.4.0 or greater and has been deactivated. Please install and activate BadgeOS and then reactivate this plugin.', 'achievement-shortcode-for-badgeos' ), admin_url( 'plugins.php' ) ) . '

'; echo '
'; // Deactivate our plugin deactivate_plugins( $this->basename ); } } } new BadgeOS_Achievement_Shortcode();