Skip to content

Instantly share code, notes, and snippets.

@flowtwo
Last active December 25, 2015 07:29
Show Gist options
  • Save flowtwo/6939821 to your computer and use it in GitHub Desktop.
Save flowtwo/6939821 to your computer and use it in GitHub Desktop.
This sends the content to http://filip.journet.sdu.dk/berta/api.php, retrieves its LIX and stores it in a custom field.
/*
* GET SAVED LIX BY ID
*/
if ( !function_exists( 'get_lix' ) ) {
function get_lix( $id = NULL ) {
global $post;
if ( is_user_logged_in() && !is_home() && !is_front_page() ) {
if ( empty( $id ) && is_object( $post ) ) {
$id = $post->ID;
}
return '<small>'. __('Word count','lix') .': '. get_post_meta( $id, 'text_word', true ) .'<br/>'. __('LIX','lix') .': '. get_post_meta( $id, 'text_lix', true ) .'</small>';
}
}
/* USE LIKE SO
* echo get_lix( 101 );
*/
}
/*
* SAVE LIX
* Thanks to @fiwa and http://filip.journet.sdu.dk/berta/api
*/
if ( !function_exists( 'save_lix' ) ) {
function save_lix() {
global $post;
if ( is_object( $post ) ) {
$text = $post->post_content;
// these may be unneccecary, but let's sanitize the content anyway
$text = strip_shortcodes( $text );
$text = strip_tags( $text );
$text = str_replace( array(' ','&nbsp;'), '%20', $text );
$text = str_replace( array(',',';','"',':','-','!','?',"'"), '', $text );
// æøå may cause confusion, so let's replace it with a single letter
$text = str_replace( array('Æ','æ','Ø','ø','Å','å'), 'o', $text );
// & is a word
$text = str_replace( '&', 'og', $text );
// let's just remove these so we're in the clear
$text = str_replace( array("\r\n", "\r", "\n"), "%20", $text );
$text = urlencode( $text );
$json = json_decode( file_get_contents( 'http://filip.journet.sdu.dk/berta/api.php?broedtekst='. $text ) );
$ord = round( $json->berta->antal_ord );
if ( !empty( $ord ) ) {
update_post_meta( $post->ID, 'text_word', $ord );
}
$lix = round( $json->berta->lix );
if ( !empty( $lix ) ) {
update_post_meta( $post->ID, 'text_lix', $lix );
}
// now the LIX is saved and we can retrieve it later with get_lix();
}
}
add_action('save_post', 'save_lix');
// add_action('draft_to_publish', 'save_lix');
// add_action('new_to_publish', 'save_lix');
// add_action('pending_to_publish', 'save_lix');
// add_action('future_to_publish', 'save_lix');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment