Created
December 26, 2018 22:18
-
-
Save jkhaui/1ae2e5b64c0c9ce2a41f99d8322190d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Medium; | |
class Citation { | |
const SHORTCODE = 'medium-editor'; | |
private $url; | |
public function __construct() | |
{ | |
ini_set( 'display_errors', 1 ); | |
error_reporting(1); | |
$this->url = plugins_url( '', realpath( __DIR__ . '/../textbox-citation.php' ) ); | |
//$this->url = plugins_url( '', realpath( __DIR__ . '/../textbox-citation.php' ) ); | |
$this->actions(); | |
} | |
public function actions() | |
{ | |
add_action( 'admin_menu', function() { | |
add_submenu_page( 'options-general.php', 'Textbox Citation', 'Textbox Citation', 'manage_options', 'textbox-citation', [ $this, 'citation_menu' ] ); | |
}); | |
add_action( 'wp_enqueue_scripts', function() { | |
wp_register_script( 'citation', $this->url . '/js/index.js', [ 'jquery' ] ); | |
wp_localize_script( 'citation', 'CITATION', [ 'nonce' => wp_create_nonce( '**B15Fa17K' ), 'ajax' => admin_url( 'admin-ajax.php' ) ] ); | |
wp_enqueue_script( 'citation' ); | |
wp_register_script( 'medium-editor2', '//cdn.jsdelivr.net/npm/medium-editor@5.23.2/dist/js/medium-editor.min.js' ); | |
wp_register_script( 'medium-insert2', $this->url . '/js/medium-insert.js', [ 'medium-editor2' ] ); | |
wp_register_style( 'medium-editor2', '//cdn.jsdelivr.net/npm/medium-editor@5.23.2/dist/css/medium-editor.min.css' ); | |
}); | |
add_action( 'wp_ajax_get_citation', [ $this, 'citation_getdata' ] ); | |
add_action( 'wp_ajax_nopriv_get_citation', [ $this, 'citation_getdata' ] ); | |
add_shortcode( self::SHORTCODE, [ $this, 'the_shortcode' ] ); | |
} | |
function citation_getdata() { | |
header( 'Content-Type: application/json' ); | |
if( !empty( $_POST[ 'data' ] ) && wp_verify_nonce( $_POST[ 'nonce' ], '**B15Fa17K' ) ) { | |
$ch = curl_init(); | |
$citation = get_option( 'citation' ); | |
if( $_POST[ 'word_boundary' ] == 'false' ) { | |
curl_setopt_array( $ch, [ | |
CURLOPT_URL => $citation[ 'api' ] . '?' . http_build_query([ 'source_type' => 'url', 'url' => $_POST[ 'data' ] ]), | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_FOLLOWLOCATION => 1 | |
]); | |
} else { | |
curl_setopt_array( $ch, [ | |
CURLOPT_URL => 'http://167.99.173.193/cgi-bin/search_book4.py?title=' . urlencode( $_POST[ 'data' ] ), | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_FOLLOWLOCATION => 1 | |
]); | |
} | |
$exec = curl_exec( $ch ); | |
if( !curl_errno( $ch ) ) { | |
$exec = json_decode( $exec ); | |
if( $exec->result == 'Success' ) { | |
$return = []; | |
$return[ 'status' ] = true; | |
if( $_POST[ 'word_boundary' ] == 'true' ) { | |
$return[ 'citation' ] = $this->citation_replace( '([firstname_author1] & [firstname_author2], [year])', $exec ); | |
$return[ 'reference' ] = $this->citation_replace( '[firstname_author1], [lastname_author1.], & [firstname_author2], [lastname_author2.] ([year]) ' . ucwords( $_POST[ 'data' ] ) . '. [city]: [publisher]', $exec ); | |
} elseif( empty( $exec->authors ) ) { | |
$return[ 'citation' ] = $this->citation_replace( '([domain_name], [year])', $exec ); | |
$return[ 'reference' ] = $this->citation_replace( '[domain_name]. ([year]). <em>[title]</em>. [online] Available at: [url] [Accessed [date].', $exec ); | |
} elseif( !empty( $exec->journal_name ) ) { | |
if( count( $exec->authors ) == 3 ) { | |
$return[ 'citation' ] = $this->citation_replace( '([lastname_author1], [lastname_author2] and [lastname_author3], [year])', $exec ); | |
} elseif( count( $exec->authors ) == 2 ) { | |
$return[ 'citation' ] = $this->citation_replace( '([lastname_author1] and [lastname_author2], [year])', $exec ); | |
} elseif( count( $exec->authors ) == 1 ) { | |
$return[ 'citation' ] = $this->citation_replace( '([lastname_author1], [year])', $exec ); | |
} else { | |
$return[ 'citation' ] = $this->citation_replace( '([lastname_author1] et al, [year])', $exec ); | |
} | |
$return[ 'reference' ] = $this->citation_replace( $citation[ 'reference' ], $exec ); | |
} else { | |
$return[ 'citation' ] = $this->citation_replace( $citation[ 'citation' ], $exec ); | |
$return[ 'reference' ] = $this->citation_replace( '[lastname_author1], [firstname_author1.] ([year]). [title]. Available at: [url] [Accessed [date]].', $exec ); | |
} | |
echo json_encode( $return ); die; | |
} | |
} | |
echo json_encode([ 'status' => false ]); | |
die; | |
} | |
} | |
function citation_replace( $content, $data ) { | |
$date = strtotime( $data->date ); | |
$authors = !empty( $data->author ) ? explode( ';', $data->author ) : $data->authors; | |
$authors = array_map( 'trim', $authors ); | |
if( !empty( $data->journal_name ) ) | |
sort( $authors ); | |
if( !empty( $authors[ 0 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 0 ] ); | |
$content = str_ireplace( '[firstname_author1]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author1]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author1.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author1.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author1[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 1 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 1 ] ); | |
$content = str_ireplace( '[firstname_author2]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author2]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author2.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author2.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author2[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 2 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 2 ] ); | |
$content = str_ireplace( '[firstname_author3]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author3]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author3.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author3.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author3[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 3 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 3 ] ); | |
$content = str_ireplace( '[firstname_author4]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author4]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author4.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author4.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author4[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 4 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 4 ] ); | |
$content = str_ireplace( '[firstname_author5]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author5]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author5.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author5.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author5[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 5 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 5 ] ); | |
$content = str_ireplace( '[firstname_author6]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author6]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author6.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author6.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author6[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 6 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 6 ] ); | |
$content = str_ireplace( '[firstname_author7]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author7]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author7.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author7.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author7[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 7 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 7 ] ); | |
$content = str_ireplace( '[firstname_author8]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author8]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author8.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author8.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author8[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $authors[ 8 ] ) ) { | |
list( $firstname, $lastname ) = explode( ' ', $authors[ 8 ] ); | |
$content = str_ireplace( '[firstname_author9]', $firstname, $content ); | |
$content = str_ireplace( '[lastname_author9]', $lastname, $content ); | |
$content = str_ireplace( '[firstname_author9.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname_author9.]', strtoupper( $lastname[ 0 ] ) . '.', $content ); | |
unset( $firstname, $lastname ); | |
} | |
else { | |
$content = preg_replace( '/&?\s?\[(firstname|lastname)_author9[\.]?\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $data->journal_name ) ) { | |
$content = str_ireplace( '[journal_name]', $data->journal_name, $content ); | |
} | |
else { | |
$content = preg_replace( '/\[journal_name\][\s]?[,]?/', '', $content ); | |
} | |
if( !empty( $data->issue ) ) { | |
$content = str_ireplace( '[issue]', $data->issue, $content ); | |
} | |
else { | |
$content = preg_replace( '/[\(]?\[issue\][\)]?[\.]?/', '', $content ); | |
} | |
$content = str_ireplace( '[volume]', ( !empty( $data->volume ) ? $data->volume : '' ), $content ); | |
/*$content = str_ireplace( '[firstname]', $firstname, $content ); | |
$content = str_ireplace( '[lastname]', $lastname, $content ); | |
$content = str_ireplace( '[firstname.]', strtoupper( $firstname[ 0 ] ) . '.', $content ); | |
$content = str_ireplace( '[lastname.]', strtoupper( $lastname[ 0 ] ) . '.', $content );*/ | |
$content = str_ireplace( '[year]', ( !empty( $data->publish_year ) ? $data->publish_year : date( 'Y', $date ) ), $content ); | |
$content = str_ireplace( '[title]', $data->title, $content ); | |
$content = str_ireplace( '[domain_name]', ucwords( parse_url( $data->url, PHP_URL_HOST ) ), $content ); | |
$content = str_ireplace( '[url]', '<a href="' . $data->url . '" data-ref-url>' . $data->url . '</a>', $content ); | |
$content = str_ireplace( '[date]', date( 'd M. Y' ), $content ); | |
if( !empty( $data->city ) ) | |
$content = str_ireplace( '[city]', trim( $data->city ), $content ); | |
if( !empty( $data->publisher ) ) | |
$content = str_ireplace( '[publisher]', $data->publisher, $content ); | |
return $content; | |
} | |
public function the_shortcode( $atts ) | |
{ | |
/*wp_enqueue_style( 'medium-editor2' ); | |
wp_enqueue_script( 'medium-insert2' );*/ | |
$atts = shortcode_atts( [ | |
'height' => 300 | |
], $atts ); | |
return '<div id="medium-editor2" class="sap-editable-area" style="height: ' . $atts[ 'height' ] . 'px; padding : 20px 10px;"></div>'; | |
} | |
function citation_menu() { | |
$data = get_option( 'citation' ); | |
if( isset( $_POST[ 'citation' ] ) ) { | |
update_option( 'citation', $_POST[ 'citation' ] ); | |
$data = $_POST[ 'citation' ]; | |
$updated = true; | |
} | |
?> | |
<div class="wrap"> | |
<h1><?php echo get_admin_page_title();?></h1> | |
<?php if( !empty( $updated ) ):?> | |
<div class="notice notice-success is-dismissible"> | |
<p><?php _e( 'Saved!' ); ?></p> | |
</div> | |
<?php endif;?> | |
<form action="" method="post"> | |
<?php wp_nonce_field( -1, 'citation_nonce' );?> | |
<div> | |
<label>API URL:</label><br /> | |
<input type="url" name="citation[api]" value="<?php echo @$data[ 'api' ]?>" /> | |
</div> | |
<div> | |
<label>Citation Format:</label><br /> | |
<textarea name="citation[citation]"><?php echo @$data[ 'citation' ]?></textarea><br /> | |
</div> | |
<div> | |
<label>Reference Format:</label><br /> | |
<textarea name="citation[reference]"><?php echo @$data[ 'reference' ]?></textarea><br /> | |
</div> | |
<div> | |
Variables:<br /> | |
<span>[firstname] - First name<br />[lastname] - Last name<br />[lastname.] - Last name abbr.<br />[firstname.] - First name abbr.<br />[year] - Year</span> | |
</div> | |
<?php submit_button();?> | |
</form> | |
</div> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment