ControlledChaos
5/7/2018 - 2:50 AM

Gist Embed Sample: PHP Class

Sample PHP code used for writing styles of embedded Gists.

<?php

/**
 * This is a PHP comment block
 * 
 * Let's put a description here so that we can see how
 * it looks with multiple lines.
 * 
 * @link http://example.com
 * @since 1.0.0
 * @package Plugin_Name
 */

namespace CCDzine_Gist_Sample\PHP;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Gist Embed Sample: PHP
 * 
 * @since 1.0.0
 */
class CCDzine_Gist_Sample_PHP {

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		// Access a function.
		$this->dependencies();

		// Add an action, old array syntax.
		add_action( 'wp_head', array( $this, 'head_action' ) );

		// Add an action, new array syntax + priority.
		add_action( 'wp_footer', [ $this, 'footer_action' ], 20 );

		// Filter some text.
		add_filter( 'some_filter', [ $this, 'filter_text' ] );

		// Arrays.
		$this->arrays();

	}

	/**
	 * Load dependencies for this plugin.
	 *
	 * @since 1.0.0
	 * @access private
	 */
	private function dependencies() {

		// Require a file once.
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/required-file.php';

		// Include a file.
		include plugin_dir_path( dirname( __FILE__ ) ) . 'includes/included_file.php';
		
	}

	/**
	 * Run via wp_head.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function head_action() {

		if ( class_exists( 'Another_Plugin' ) ) {

			// Let's echo a style block.
			$style =
				'<style>
					body {
						background: #fff;
					}

					.class_selector {
						color: #000;
					}

					#id_selector {
						border: solid 1px #ccc;
					}
				</style>';

		} else {

			$style = '';

		}

		echo apply_filters( 'style_filter', $style );

	}

	/**
	 * Run via wp_footer.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function footer_action() { ?>

		<script>
			// Here is some script added directly.
			jQuery(document).ready( function($) {
				$( 'a.cool-link' ).addClass( 'even-cooler' );
			} );
		</script>

	<?php }

	/**
	 * Text to be filtered.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function filter_text() {

		$text = sprintf( '<h1>%1s</h1>', esc_html__( 'Gist Embed Sample: PHP', 'ccdzine' ) );

		return $text;

	}

	/**
	 * Let's test some arrays.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public static function arrays() {

		$one = array( 'controlled', 'chaos', 'design' );
		$two = [
			'make'  => __( 'Datsun' ),
			'model' => __( '510' ),
			'year'  => __( '1969' )
		];

		$output = $one;
		$output .= $two;

		return $output;

	}

}

$gist_sample_php = new CCDzine_Gist_Sample_PHP();

Gist Embed Sample: PHP Class

Code is for display purposes only.