<commit_before><?php
/**
 * Credly Badge Builder Class
 *
 * @package BadgeOS
 * @subpackage Classes
 * @author Credly, LLC
 * @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
 * @link https://credly.com
 */

class Credly_Badge_Builder {

	/**
	 * SDK URL for accessing badge builder.
	 * @var string
	 */
	public $sdk_url = 'https://credly.com/badge-builder/';

	/**
	 * The site's Credly API key (false if none).
	 * @var string|bool
	 */
	public $credly_api_key = '';

	/**
	 * Temp token used for running badge builder.
	 * @var string
	 */
	public $temp_token = '';

	/**
	 * Instantiate the badge builder.
	 *
	 * @since 1.3.0
	 *
	 */
	public function __construct() {

		// Fetch the credly API key
		$this->credly_api_key = credly_get_api_key();

		add_action( 'wp_ajax_badge-builder-save-badge', array( $this, 'ajax_save_badge' ) );
		add_action( 'wp_ajax_badge-builder-generate-link', array( $this, 'ajax_generate_link' ) );

	}

	/**
	 * Fetch the temp badge builder token.
	 *
	 * @since 1.3.0
	 */
	public function fetch_temp_token() {
		global $wp_version;

		// If we have a valid Credly API key
		if ( $this->credly_api_key ) {
			// Trade the key for a temp token
			$response = wp_remote_post(
				trailingslashit( $this->sdk_url ) . 'code',
				array(
					'user-agent' => 'WordPress/' . $wp_version . '; BadgeOS/' . BadgeOS::$version . '; ' . get_bloginfo( 'url' ),
					'body' => array(
						'access_token' => $this->credly_api_key
					)
				)
			);

			// If the response was not an error
			if ( ! is_wp_error( $response ) ) {
				// Decode the response
				$response_body = json_decode( $response['body'] );

				// If response was successful, return the temp token
				if ( isset( $response_body->temp_token ) )
					return $response_body->temp_token;
			}
		}

		// If we made it here, we couldn't retrieve a token
		return false;
	}

	/**
	 * Generate the badge builder link URI.
	 *
	 * @since  1.3.0
	 *
	 * @param  array  $args Args for building the link.
	 * @return string       URL for loading a badge builder.
	 */
	public function generate_link( $args = array() ) {

		// Setup and parse our default args
		$defaults = apply_filters( 'credly_badge_builder_generate_link_defaults', array(
			'continue'  => null,
		) );
		$args = wp_parse_args( $args, $defaults );

		if ( $this->credly_api_key ) {
			$link = add_query_arg(
				array(
					'continue'  => rawurlencode( json_encode( $args['continue'] ) )
				),
				esc_url( trailingslashit( $this->sdk_url ) . 'embed/' . $this->fetch_temp_token() )
			);
		} else {
			$link = '#TB_inline?width=' . $args['width'] . '&height=' . $args['width'] . '&inlineId=teaser';
		}

		// Return our generated link
		return $link;
	}

	/**
	 * Render the badge builder link.
	 *
	 * @since  1.3.0
	 *
	 * @param  array  $args Args for setting link attributes.
	 * @return string       HTML markup for anchor tag.
	 */
	public function render_link( $args = array() ) {

		// Setup and parse our default args
		$defaults = apply_filters( 'credly_badge_builder_render_link_defaults', array(
			'attachment_id' => get_post_thumbnail_id(),
			'width'         => '960',
			'height'        => '540',
			'continue'      => null,
			'link_text'     => __( 'Use Credly Badge Builder', 'badgeos' ),
		) );
		$args = wp_parse_args( $args, $defaults );

		// Build our link tag
		if ( $this->credly_api_key )
			$link = admin_url( 'admin-ajax.php?action=badge-builder-generate-link&attachment_id=' . $args['attachment_id'] . '&TB_iframe=true' );
		else
			$link = $link = '#TB_inline?width=' . $args['width'] . '&height=' . $args['width'] . '&inlineId=teaser';
		$output = '<a href="' . $link . '" class="thickbox badge-builder-link" data-width="' . $args['width'] . '" data-height="' . $args['height'] . '" data-attachment_id="' . $args['attachment_id'] . '">' . $args['link_text'] . '</a>';

		// Include teaser output if we have no API key
		if ( ! $this->credly_api_key )
			$output .= '<div id="teaser" style="display:none;"><a href="' . admin_url( 'admin.php?page=badgeos_sub_credly_integration' ) . '"><img src="' . $GLOBALS['badgeos']->directory_url . 'images/badge-builder-teaser.png" alt="Enable Credly Integration to use the Badge Builder"></a></div>';<commit_msg>Only attempt to get thumbnail ID when viewing a post<commit_after><?php
/**
 * Credly Badge Builder Class
 *
 * @package BadgeOS
 * @subpackage Classes
 * @author Credly, LLC
 * @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
 * @link https://credly.com
 */

class Credly_Badge_Builder {

	/**
	 * SDK URL for accessing badge builder.
	 * @var string
	 */
	public $sdk_url = 'https://credly.com/badge-builder/';

	/**
	 * The site's Credly API key (false if none).
	 * @var string|bool
	 */
	public $credly_api_key = '';

	/**
	 * Temp token used for running badge builder.
	 * @var string
	 */
	public $temp_token = '';

	/**
	 * Instantiate the badge builder.
	 *
	 * @since 1.3.0
	 *
	 */
	public function __construct() {

		// Fetch the credly API key
		$this->credly_api_key = credly_get_api_key();

		add_action( 'wp_ajax_badge-builder-save-badge', array( $this, 'ajax_save_badge' ) );
		add_action( 'wp_ajax_badge-builder-generate-link', array( $this, 'ajax_generate_link' ) );

	}

	/**
	 * Fetch the temp badge builder token.
	 *
	 * @since 1.3.0
	 */
	public function fetch_temp_token() {
		global $wp_version;

		// If we have a valid Credly API key
		if ( $this->credly_api_key ) {
			// Trade the key for a temp token
			$response = wp_remote_post(
				trailingslashit( $this->sdk_url ) . 'code',
				array(
					'user-agent' => 'WordPress/' . $wp_version . '; BadgeOS/' . BadgeOS::$version . '; ' . get_bloginfo( 'url' ),
					'body' => array(
						'access_token' => $this->credly_api_key
					)
				)
			);

			// If the response was not an error
			if ( ! is_wp_error( $response ) ) {
				// Decode the response
				$response_body = json_decode( $response['body'] );

				// If response was successful, return the temp token
				if ( isset( $response_body->temp_token ) )
					return $response_body->temp_token;
			}
		}

		// If we made it here, we couldn't retrieve a token
		return false;
	}

	/**
	 * Generate the badge builder link URI.
	 *
	 * @since  1.3.0
	 *
	 * @param  array  $args Args for building the link.
	 * @return string       URL for loading a badge builder.
	 */
	public function generate_link( $args = array() ) {

		// Setup and parse our default args
		$defaults = apply_filters( 'credly_badge_builder_generate_link_defaults', array(
			'continue'  => null,
		) );
		$args = wp_parse_args( $args, $defaults );

		if ( $this->credly_api_key ) {
			$link = add_query_arg(
				array(
					'continue'  => rawurlencode( json_encode( $args['continue'] ) )
				),
				esc_url( trailingslashit( $this->sdk_url ) . 'embed/' . $this->fetch_temp_token() )
			);
		} else {
			$link = '#TB_inline?width=' . $args['width'] . '&height=' . $args['width'] . '&inlineId=teaser';
		}

		// Return our generated link
		return $link;
	}

	/**
	 * Render the badge builder link.
	 *
	 * @since  1.3.0
	 *
	 * @param  array  $args Args for setting link attributes.
	 * @return string       HTML markup for anchor tag.
	 */
	public function render_link( $args = array() ) {
		global $post;

		// Setup and parse our default args
		$defaults = apply_filters( 'credly_badge_builder_render_link_defaults', array(
			'attachment_id' => $post ? get_post_thumbnail_id() : 0,
			'width'         => '960',
			'height'        => '540',
			'continue'      => null,
			'link_text'     => __( 'Use Credly Badge Builder', 'badgeos' ),
		) );
		$args = wp_parse_args( $args, $defaults );

		// Build our link tag
		if ( $this->credly_api_key )
			$link = admin_url( 'admin-ajax.php?action=badge-builder-generate-link&attachment_id=' . $args['attachment_id'] . '&TB_iframe=true' );
		else
			$link = $link = '#TB_inline?width=' . $args['width'] . '&height=' . $args['width'] . '&inlineId=teaser';
		$output = '<a href="' . $link . '" class="thickbox badge-builder-link" data-width="' . $args['width'] . '" data-height="' . $args['height'] . '" data-attachment_id="' . $args['attachment_id'] . '">' . $args['link_text'] . '</a>';

		// Include teaser output if we have no API key
		if ( ! $this->credly_api_key )
			$output .= '<div id="teaser" style="display:none;"><a href="' . admin_url( 'admin.php?page=badgeos_sub_credly_integration' ) . '"><img src="' . $GLOBALS['badgeos']->directory_url . 'images/badge-builder-teaser.png" alt="Enable Credly Integration to use the Badge Builder"></a></div>';
{'fraction_non_alphanumeric': 0.16388272398084336, 'fraction_numerical': 0.004789160144842892, 'mean_word_length': 3.1045062320230103, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 61, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5093824', 'n_tokens_mistral': 3355, 'n_tokens_neox': 2932, 'n_words': 730}