__( 'Add content recomendations to your site.', 'algorithmia_widget_domain' ), ) ); } // Creating widget front-end. public function widget( $args, $instance ) { // We only display on single post pages, not on the homepage, user pages, etc. if ( is_single() == false ) { return false; } $title = apply_filters( 'widget_title', $instance['title'] ); // Before and after widget arguments are defined by themes. echo $args['before_widget']; if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } // This is the Algorithmia API key. $api_key = $instance['api_key']; // This is the number of related pages we will request from the API. $post_count = $instance['post_count']; // We need the site name to prevent the display of the site name in the links that the Algorithmia API returns. // Algorithmia API expects this as the content value in the meta tag. We pass this below. $site_name = get_bloginfo('name', 'raw'); $name_no_spaces = str_replace(' ', '', $site_name); if ( ! empty( $api_key ) ) { echo '
'; echo $args['after_widget']; } } // Widget form. public function form( $instance ) { $api_key = ''; // Handle title field. if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'Recommended Content', 'algorithmia_widget_domain' ); } // Handle API Key Field. if ( isset($instance['api_key']) ) { $api_key = $instance['api_key']; } if ( !isset($instance['api_key']) ) { $api_key = ''; } // Handle Disable images field. if ( isset($instance['disable_images'] ) ) { $disable_images = $instance['disable_images']; } else { $disable_images = false; $disable_images = $instance['disable_images']; } // Hande disable summaries field. if ( isset($instance['disable_summaries'] ) ) { $disable_summaries = $instance['disable_summaries']; } else { $disable_summaries = false; $instance['disable_summaries'] = false; } // Handle disable subtitles field. if ( isset($instance['disable_subtitles'] ) ) { $disable_subtitles = $instance['disable_subtitles']; } else { $disable_subtitles = false; $instance['disable_subtitles'] = false; } // Handle post count field. if ( isset($instance['post_count']) ) { $post_count = $instance['post_count']; } if ( !isset($instance['post_count']) ) { $post_count = 10; $instance['post_count'] = 10; } // Widget admin form ?>

id="get_field_id( 'disable_images' ); ?>" name="get_field_name( 'disable_images' ); ?>" />

id="get_field_id( 'disable_summaries' ); ?>" name="get_field_name( 'disable_summaries' ); ?>" />

id="get_field_id( 'disable_subtitles' ); ?>" name="get_field_name( 'disable_subtitles' ); ?>" />

"; echo $output; } } add_action( 'load-post.php', 'algorithmia_post_meta_boxes_setup' ); add_action( 'load-post-new.php', 'algorithmia_post_meta_boxes_setup' ); /* Create one or more meta boxes to be displayed on the post editor screen. */ function algorithmia_add_post_meta_boxes() { $screens = array( 'post', 'page' ); foreach( $screens as $screen ) { add_meta_box( 'algorithmia-post-options', // Unique ID esc_html__( 'Recommender Options', 'example' ), // Title 'algorithmia_post_options_meta_box', // Callback function $screen, // Admin page (or post type) 'side', // Context 'default' // Priority ); } } /* Display the post meta box. */ function algorithmia_post_options_meta_box( $object, $box ) { ?>


size="30" />

post_type ); /* Check if the current user has permission to edit the post. */ if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; /* Get the posted data and sanitize it for use as an HTML class. */ $new_meta_value = ( isset( $_POST['algorithmia-post-options'] ) ); echo print_r($new_meta_value, true); /* Get the meta key. */ $meta_key = 'algorithmia_exclude_post'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); } /** * Include CSS file to optionally hide recommendation images. * We must hook into 'init' because by the time the widget loads, it's too late to load CSS files. * This option is set in the widget UI by site admins. */ function algorithmia_register_css() { // We first register all the styles. This does not enable them. wp_register_style( 'algorithmia-disable-images', plugin_dir_url( __FILE__ ) . 'algorithmia-disable-images.css' ); wp_register_style( 'algorithmia-disable-summaries', plugin_dir_url( __FILE__ ) . 'algorithmia-disable-summaries.css' ); wp_register_style( 'algorithmia-disable-subtitles', plugin_dir_url( __FILE__ ) . 'algorithmia-disable-subtitles.css' ); // We check to see if the widget has been configured to disable each display option, then enqueue a unique style sheet for each. $bool = get_option('algorithmia_disable_images'); if ( $bool == 1 ) { wp_enqueue_style( 'algorithmia-disable-images' ); } $bool2 = get_option('algorithmia_disable_summaries'); if ( $bool2 == 1 ) { wp_enqueue_style( 'algorithmia-disable-summaries' ); } $bool3 = get_option('algorithmia_disable_subtitles'); if ( $bool3 == 1 ) { wp_enqueue_style( 'algorithmia-disable-subtitles' ); } } add_action( 'init', 'algorithmia_register_css' );