git-it-write/includes/shortcodes.php
Aakash Chakravarthy 229b6fa412 v1.5 changes
2022-09-01 20:36:29 +05:30

55 lines
No EOL
1.3 KiB
PHP

<?php
if( ! defined( 'ABSPATH' ) ) exit;
class GIW_Shortcodes{
public static function init(){
add_shortcode( 'giw_edit_link', array( __CLASS__, 'edit_link' ) );
}
public static function edit_link( $atts ){
global $post;
$current_post_id = '';
if( is_object( $post ) ){
$current_post_id = $post->ID;
}
$atts = shortcode_atts( array(
'post_id' => $current_post_id,
'text' => 'Edit this page',
'icon' => '<i class="fas fa-pen"></i> &nbsp; ',
'auto_p' => false
), $atts );
if( empty( $atts[ 'post_id' ] ) ){
return '';
}
$meta = get_post_meta( $atts[ 'post_id' ], '', true );
if( !array_key_exists( 'github_url', $meta ) || empty( $meta[ 'github_url' ][0] ) ){
return '';
}
$github_url = $meta[ 'github_url' ][0];
$link = '<a href="' . esc_url( $github_url ) . '" class="giw-edit_link" target="_blank" rel="noreferrer noopener">' . wp_kses_post( $atts[ 'icon' ] ) . esc_html( $atts[ 'text' ] ) . '</a>';
if( $atts[ 'auto_p' ] ){
return '<p>' . $link . '</p>';
}else{
return $link;
}
}
}
GIW_Shortcodes::init();
?>