'. __( 'Save this record before attempting to add a related record.', 'scrib' ) .'
';
}
}
public function meditor_save_post($post_id, $post) {
if ( $post_id && is_array( $_REQUEST['scrib_meditor'] )){
// make sure meta is added to the post, not a revision
if ( $the_post = wp_is_post_revision( $post_id ))
$post_id = $the_post;
$record = is_array( get_post_meta( $post_id, 'scrib_meditor_content', true )) ? get_post_meta( $post_id, 'scrib_meditor_content', true ) : array();
if( is_array( $_REQUEST['scrib_meditor'] )){
foreach( $_REQUEST['scrib_meditor'] as $key => &$val )
unset( $record[ $this->meditor_input->form_key ] );
$record = $this->meditor_merge_meta( $record, $this->meditor_sanitize_input( $_REQUEST['scrib_meditor'] ));
add_post_meta( $post_id, 'scrib_meditor_content', $record, TRUE ) or update_post_meta( $post_id, 'scrib_meditor_content', $record );
do_action( 'scrib_meditor_save_record', $post_id, $record );
}
/*
foreach( $_REQUEST['scrib_meditor'] as $this->meditor_input->form_key => $this->meditor_input->form ){
unset( $record[ $this->meditor_input->form_key ] );
foreach( $this->meditor_input->form as $this->meditor_input->group_key => $this->meditor_input->group )
foreach( $this->meditor_input->group as $this->meditor_input->iteration_key => $this->meditor_input->iteration )
foreach( $this->meditor_input->iteration as $this->meditor_input->key => $this->meditor_input->val ){
if( is_callable( $this->meditor_forms[ $this->meditor_input->form_key ]['_elements'][ $this->meditor_input->group_key ]['_elements'][ $this->meditor_input->key ]['_sanitize'] )){
$filtered = FALSE;
$filtered = call_user_func( $this->meditor_forms[ $this->meditor_input->form_key ]['_elements'][ $this->meditor_input->group_key ]['_elements'][ $this->meditor_input->key ]['_sanitize'] , stripslashes( $this->meditor_input->val ));
if( !empty( $filtered ))
$record[ $this->meditor_input->form_key ][ $this->meditor_input->group_key ][ $this->meditor_input->iteration_key ][ $this->meditor_input->key ] = stripslashes( $filtered );
}else{
if( !empty( $record[ $this->meditor_input->form_key ][ $this->meditor_input->group_key ][ $this->meditor_input->key ][ $this->meditor_input->iteration_key ][ $this->meditor_input->key ] ))
$record[ $this->meditor_input->form_key ][ $this->meditor_input->group_key ][ $this->meditor_input->key ][ $this->meditor_input->iteration_key ][ $this->meditor_input->key ] = stripslashes( $this->meditor_input->val );
}
}
}
add_post_meta( $post_id, 'scrib_meditor_content', $record, TRUE ) or update_post_meta( $post_id, 'scrib_meditor_content', $record );
do_action( 'scrib_meditor_save_record', $post_id, $record );
*/
}
}
public function meditor_merge_meta( $orig = array(), $new = array(), $nsourceid = FALSE ){
$orig = apply_filters( 'scrib_meditor_premerge_old', $orig , $nsourceid );
$new = apply_filters( 'scrib_meditor_premerge_new', $new , $nsourceid );
if( $forms = array_intersect( array_keys( $orig ), array_keys( $new ))){
$return = array();
foreach( $forms as $form ){
$sections = array_unique( array_merge( array_keys( $orig[ $form ] ), array_keys( $new[ $form ] )));
foreach( $sections as $section ){
// preserve the bits that are to be suppressed
$suppress = array();
foreach( $orig[ $form ][ $section ] as $key => $val )
if( $val['suppress'] )
$suppress[ $form ][ $section ][ $key ] = $val;
// remove metadata that's sourced from the new sourceid
if( $nsourceid )
foreach( $orig[ $form ][ $section ] as $key => $val )
if( isset( $val['src'] ) && ( $val['src'] == $nsourceid ))
unset( $orig[ $form ][ $section ][ $key ] );
$return[ $form ][ $section ] = $this->array_unique_deep( array_merge( count( $new[ $form ][ $section ] ) ? $new[ $form ][ $section ] : array() , count( $orig[ $form ][ $section ] ) ? $orig[ $form ][ $section ] : array() , $suppress ));
}
}
if( $diff = array_diff( array_keys( $orig ), array_keys( $new ))){
foreach( $diff as $form )
$return[ $form ] = array_merge( is_array( $orig[ $form ] ) ? $orig[ $form ] : array(), is_array( $new[ $form ] ) ? $new[ $form ] : array() );
}
return( $return );
}else{
return( array_merge( is_array( $orig ) ? $orig : array(), is_array( $new ) ? $new : array() ));
}
}
public function meditor_sanitize_input( &$input ){
$record = array();
foreach( $input as $this->meditor_input->form_key => $this->meditor_input->form ){
foreach( $this->meditor_input->form as $this->meditor_input->group_key => $this->meditor_input->group )
foreach( $this->meditor_input->group as $this->meditor_input->iteration_key => $this->meditor_input->iteration )
foreach( $this->meditor_input->iteration as $this->meditor_input->key => $this->meditor_input->val ){
if( is_callable( $this->meditor_forms[ $this->meditor_input->form_key ]['_elements'][ $this->meditor_input->group_key ]['_elements'][ $this->meditor_input->key ]['_sanitize'] )){
$filtered = FALSE;
$filtered = call_user_func( $this->meditor_forms[ $this->meditor_input->form_key ]['_elements'][ $this->meditor_input->group_key ]['_elements'][ $this->meditor_input->key ]['_sanitize'] , stripslashes( $this->meditor_input->val ));
if( !empty( $filtered ))
$record[ $this->meditor_input->form_key ][ $this->meditor_input->group_key ][ $this->meditor_input->iteration_key ][ $this->meditor_input->key ] = stripslashes( $filtered );
}else{
if( !empty( $record[ $this->meditor_input->form_key ][ $this->meditor_input->group_key ][ $this->meditor_input->key ][ $this->meditor_input->iteration_key ][ $this->meditor_input->key ] ))
$record[ $this->meditor_input->form_key ][ $this->meditor_input->group_key ][ $this->meditor_input->key ][ $this->meditor_input->iteration_key ][ $this->meditor_input->key ] = stripslashes( $this->meditor_input->val );
}
}
}
return apply_filters( 'scrib_meditor_sanitize_record', $record );
}
public function meditor_sanitize_month( $val ){
if( !is_numeric( $val ) && !empty( $val )){
if( strtotime( $val .' 2008' ))
return( date( 'm', strtotime( $val .' 2008' )));
}else{
$val = absint( $val );
if( $val > 0 && $val < 13 )
return( $val );
}
return( FALSE );
}
public function meditor_sanitize_day( $val ){
$val = absint( $val );
if( $val > 0 && $val < 32 )
return( $val );
return( FALSE );
}
public function meditor_sanitize_selectlist( $val ){
if( array_key_exists( $val, $this->meditor_forms[ $this->meditor_input->form_key ]['_elements'][ $this->meditor_input->group_key ]['_elements'][ $this->meditor_input->key ]['_input']['_values'] ))
return( $val );
return( FALSE );
}
public function meditor_sanitize_punctuation( $str )
{
// props to K. T. Lam of HKUST
$str = html_entity_decode( $str );
/*
//strip html entities, i.e. ;
$htmlentity = '\&\#\d\d\;';
$lead_htmlentity_pattern = '/^'.$htmlentity.'/';
$trail_htmlentity_pattern = '/'.$htmlentity.'$/';
$str = preg_replace($lead_htmlentity_pattern, '', preg_replace($trail_htmlentity_pattern, '', $str));
*/
//strip ASCII punctuations
$puncts = '\s\~\!\@\#\$\%\^\&\*\_\+\`\-\=\{\}\|\[\]\\\:\"\;\'\<\>\?\,\.\/';
$lead_puncts_pattern = '/^['.$puncts.']+/';
$trail_puncts_pattern = '/['.$puncts.']+$/';
$str = preg_replace($trail_puncts_pattern, '', preg_replace($lead_puncts_pattern, '', $str));
//strip repeated white space
$puncts_pattern = '/[\s]+/';
$str = preg_replace( $puncts_pattern, ' ', $str );
//strip white space before punctuations
$puncts_pattern = '/[\s]+([\~\!\@\#\$\%\^\&\*\_\+\`\-\=\{\}\|\[\]\\\:\"\;\'\<\>\?\,\.\/])+/';
$str = preg_replace( $puncts_pattern, '\1', $str );
//Strip ()
$both_pattern = '/^[\(]([^\(|\)]+)[\)]$/';
$trail_pattern = '/^([^\(]+)[\)]$/';
$lead_pattern = '/^[\(]([^\)]+)$/';
$str = preg_replace($lead_pattern, '\\1', preg_replace($trail_pattern,'\\1', preg_replace($both_pattern, '\\1', $str)));
return $str;
}
public function meditor_sanitize_related( $val ){
if( is_numeric( $val ) && get_permalink( absint( $val )) )
return( absint( $val ) );
if( $url = sanitize_url( $val) ){
if( $post_id = url_to_postid( $url ) )
return( $post_id );
else
return( $url );
}
return( FALSE );
}
public function meditor_strip_initial_articles( $content ) {
// TODO: add more articles, such as those from here: http://www.loc.gov/marc/bibliographic/bdapndxf.html
return( preg_replace( $this->initial_articles, '', $content ));
}
public function meditor_pre_save_filters( $content ) {
if ( is_array( $_REQUEST['scrib_meditor'] )){
switch( current_filter() ){
case 'pre_post_title':
return( apply_filters( 'scrib_meditor_pre_title', $content, $_REQUEST['scrib_meditor'] ));
break;
case 'pre_post_excerpt':
return( apply_filters( 'scrib_meditor_pre_excerpt', $content, $_REQUEST['scrib_meditor'] ));
break;
case 'pre_post_content':
default:
return( apply_filters( 'scrib_meditor_pre_content', $content, $_REQUEST['scrib_meditor'] ));
}
}
return( $content );
}
public function meditor_register_menus(){
if( ( 'post-new.php' == basename( $_SERVER['PHP_SELF'] )) && ( isset( $_GET['posted'] ) ) && ( !isset( $_GET['scrib_meditor_add'] ) ) && ( $form = key( get_post_meta( $_GET['posted'], 'scrib_meditor_content', true )) ) ){
$_GET['scrib_meditor_add'] = 'sibling';
$_GET['scrib_meditor_from'] = $_GET['posted'];
die( wp_redirect( admin_url( 'post-new.php' ) .'?'. http_build_query( $_GET ) ));
}
}
public function meditor_register( $handle , $prototype ){
add_action( 'admin_menu', array( &$this, 'meditor_register_menus' ));
if( isset( $this->meditor_forms[ $handle ] ))
return( FALSE );
$this->meditor_forms[ $handle ] = $prototype;
}
public function meditor_unregister( $handle ){
if( !isset( $this->meditor_forms[ $handle ] ))
return( FALSE );
unset( $this->meditor_forms[ $handle ] );
}
public function meditor_footer_activatejs(){
?>
'. $content .'');
}
public function meditor_suggest_tags(){
if ( isset( $_GET['tax'] )){
$taxonomy = explode(',', $_GET['tax'] );
$taxonomy = array_filter( array_map( 'sanitize_title', array_map( 'trim', $taxonomy )));
}else{
$taxonomy = $this->taxonomies_for_suggest;
}
$s = sanitize_title( trim( $_REQUEST['q'] ));
if ( strlen( $s ) < 2 )
$s = '';
$cachekey = md5( $s . implode( $taxonomy ));
if( !$suggestion = wp_cache_get( $cachekey , 'scrib_suggest_meditor' )){
if ( empty( $s ) ){
foreach( get_terms( $taxonomy, array( 'number' => 25, 'orderby' => 'count', 'order' => 'DESC' ) ) as $term )
$suggestion[] = $term->name;
$suggestion = implode( $suggestion, "\n" );
}else{
global $wpdb;
$suggestion = implode( array_unique( $wpdb->get_col( "SELECT t.name, ((( 100 - t.len ) + 1 ) * tt.count ) AS hits
FROM
(
SELECT term_id, name, LENGTH(name) AS len
FROM $wpdb->terms
WHERE slug LIKE ('" . $s . "%')
ORDER BY len ASC
LIMIT 100
) t
JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id
WHERE tt.taxonomy IN('" . implode( "','", $taxonomy ). "')
AND tt.count > 0
ORDER BY hits DESC
LIMIT 11;
")), "\n" );
}
wp_cache_set( $cachekey , $suggestion, 'scrib_suggest_meditor', 1800 );
}
echo $suggestion;
die;
}
public function import_post_exists( $idnumbers ) {
global $wpdb;
$post_id = FALSE;
$post_ids = $tt_ids = array();
foreach( $idnumbers as $idnum )
$tt_ids[] = get_term( (int) is_term( (string) $idnum['id'] ), $idnum['type'] );
if( count( $tt_ids )){
foreach( $tt_ids as $k => $tt_id )
if( isset( $tt_id->term_taxonomy_id ))
$tt_ids[ $k ] = (int) $tt_id->term_taxonomy_id;
else
unset( $tt_ids[ $k ] );
if( !count( $tt_ids ))
return( FALSE );
$post_ids = $wpdb->get_col( "SELECT object_id, COUNT(*) AS hits
FROM $wpdb->term_relationships
WHERE term_taxonomy_id IN ('". implode( '\',\'', $tt_ids ) ."')
GROUP BY object_id
ORDER BY hits DESC
LIMIT 100" );
if( 1 < count( $post_ids )){
// de-index the duplicate posts
// TODO: what if they have comments? What if others have linked to them?
$this->import_deindex_post( $post_ids );
// usleep( 250000 ); // give the database a moment to settle
}
foreach( $post_ids as $post_id )
if( get_post( $post_id ))
return( $post_id );
}
return( FALSE );
}
public function import_deindex_post( $post_ids ){
// sets a post's status to draft so that it no longer appears in searches
// TODO: need to find a better status to hide it from searches,
// but not invalidate incoming links or remove comments
global $wpdb;
foreach( (array) $post_ids as $post_id ){
$post_id = absint( $post_id );
if( !$post_id )
continue;
// set the post to draft (TODO: use a WP function instead of writing to DB)
$wpdb->get_results( "UPDATE $wpdb->posts SET post_status = 'draft' WHERE ID = $post_id" );
// clear the post/page cache
clean_page_cache( $post_id );
clean_post_cache( $post_id );
// do the post transition
wp_transition_post_status( 'draft', 'publish', $post_id );
}
}
public function import_insert_post( $bibr ){
// return(1);
global $wpdb, $bsuite;
if( !defined( 'DOING_AUTOSAVE' ) )
define( 'DOING_AUTOSAVE', TRUE ); // prevents revision tracking
wp_defer_term_counting( TRUE ); // may improve performance
remove_filter( 'content_save_pre', array( &$bsuite, 'innerindex_nametags' )); // don't build an inner index for catalog records
remove_filter( 'publish_post', '_publish_post_hook', 5, 1 ); // avoids pinging links in catalog records
remove_filter( 'save_post', '_save_post_hook', 5, 2 ); // don't bother
// kses_remove_filters(); // don't kses filter catalog records
define( 'WP_IMPORTING', TRUE ); // may improve performance by preventing exection of some unknown hooks
$postdata = array();
if( $this->import_post_exists( $bibr['_idnumbers'] )){
$postdata['ID'] = $this->import_post_exists( $bibr['_idnumbers'] );
$oldrecord = get_post_meta( $postdata['ID'], 'scrib_meditor_content', true );
//TODO: setting post title and content at this point works, but it ignores the opportunity to merge data from the existing record.
$postdata['post_title'] = apply_filters( 'scrib_meditor_pre_title', strlen( get_post_field( 'post_title', $postdata['ID'] )) ? get_post_field( 'post_title', $postdata['ID'] ) : $bibr['_title'], $bibr );
$postdata['post_content'] = apply_filters( 'scrib_meditor_pre_content', strlen( get_post_field( 'post_content', $postdata['ID'] )) ? get_post_field( 'post_content', $postdata['ID'] ) : $bibr['_body'], $bibr );
if( isset( $bibr['_acqdate'] ))
$postdata['post_date'] =
$postdata['post_date_gmt'] =
$postdata['post_modified'] =
$postdata['post_modified_gmt'] = strlen( get_post_field( 'post_date', $postdata['ID'] )) ? get_post_field( 'post_date', $postdata['ID'] ) : $bibr['_acqdate'];
}else{
$postdata['post_title'] = apply_filters( 'scrib_meditor_pre_title', $bibr['_title'], $bibr );
$postdata['post_content'] = apply_filters( 'scrib_meditor_pre_content', $bibr['_body'], $bibr );
if( isset( $bibr['_acqdate'] ))
$postdata['post_date'] =
$postdata['post_date_gmt'] =
$postdata['post_modified'] =
$postdata['post_modified_gmt'] = $bibr['_acqdate'];
}
$postdata['comment_status'] = get_option('default_comment_status');
$postdata['ping_status'] = get_option('default_ping_status');
$postdata['post_status'] = 'publish';
$postdata['post_type'] = 'post';
$postdata['post_author'] = $this->options['catalog_author_id'];
if( isset( $bibr['_icon'] ))
$the_icon = $bibr['_icon'];
$nsourceid = $bibr['_sourceid'];
unset( $bibr['_title'] );
unset( $bibr['_acqdate'] );
unset( $bibr['_idnumbers'] );
unset( $bibr['_sourceid'] );
unset( $bibr['_icon'] );
$postdata['post_excerpt'] = '';
if( empty( $postdata['post_title'] ))
return( FALSE );
//echo "
Pre
";
//print_r( $bibr );
//die;
// sanitize the input record
$bibr = $this->meditor_sanitize_input( $bibr );
//echo "
Sanitized
";
//print_r( $bibr );
// merge it with the old record
if( is_array( $oldrecord ))
$bibr = $this->meditor_merge_meta( $oldrecord, $bibr, $nsourceid );
//echo "
Merged
";
//print_r( $bibr );
$post_id = wp_insert_post( $postdata ); // insert the post
if($post_id){
add_post_meta( $post_id, 'scrib_meditor_content', $bibr, TRUE ) or update_post_meta( $post_id, 'scrib_meditor_content', $bibr );
do_action( 'scrib_meditor_save_record', $post_id, $bibr );
if( isset( $the_icon )){
if( is_array( $the_icon ))
add_post_meta( $post_id, 'bsuite_post_icon', $the_icon, TRUE ) or update_post_meta( $post_id, 'bsuite_post_icon', $the_icon );
else if( is_string( $the_icon ))
$bsuite->icon_resize( $the_icon, $post_id, TRUE );
}
return( $post_id );
}
return(FALSE);
}
public function import_harvest_tobepublished_count() {
global $wpdb;
return( $wpdb->get_var( 'SELECT COUNT(*) FROM '. $this->harvest_table .' WHERE imported = 0' ));
}
public function import_harvest_publish() {
global $wpdb;
$interval = 25;
if( isset( $_GET[ 'n' ] ) == false ) {
$n = 0;
} else {
$n = absint( $_GET[ 'n' ] );
}
$posts = $wpdb->get_results('SELECT * FROM '. $this->harvest_table .' WHERE imported = 0 ORDER BY enriched DESC LIMIT 0,'. $interval, ARRAY_A);
if( is_array( $posts )) {
echo "
Fetching records in batches of $interval...publishing them...making coffee. Please be patient.
';
$scrib->editsearch();
if ( !empty( $search_text_bottom ) )
echo '
' . $search_text_bottom . '
';
}
echo $after_widget;
}
function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['search-title'] = wp_filter_nohtml_kses( $new_instance['search-title'] );
$instance['search-text-top'] = wp_filter_post_kses( $new_instance['search-text-top'] );
$instance['search-text-bottom'] = wp_filter_post_kses( $new_instance['search-text-bottom'] );
$instance['browse-title'] = wp_filter_nohtml_kses( $new_instance['browse-title'] );
$instance['browse-text-top'] = wp_filter_post_kses( $new_instance['browse-text-top'] );
$instance['browse-text-bottom'] = wp_filter_post_kses( $new_instance['browse-text-bottom'] );
$instance['default-title'] = wp_filter_nohtml_kses( $new_instance['default-title'] );
$instance['default-text'] = wp_filter_post_kses( $new_instance['default-text'] );
return $instance;
}
function form( $instance )
{
//Defaults
$instance = wp_parse_args( (array) $instance,
array(
'search-title' => 'Searching Our Collection',
'search-text-top' => 'Your search found [scrib_hit_count] items with all of the following terms:',
'search-text-bottom' => 'Click [x] to remove a term, or use the facets in the sidebar to narrow your search. What are facets? Results sorted by keyword relevance.',
'browse-title' => 'Browsing Our Collection',
'browse-text-top' => 'We have [scrib_hit_count] items with all of the following terms:',
'browse-text-bottom' => 'Click [x] to remove a term, or use the facets in the sidebar to narrow your search. What are facets? Results sorted by the date added to the collection.',
'default-title' => 'Browsing Our Collection',
'default-text' => 'We have [scrib_hit_count] books, CDs, DVDs, and other materials in our collection. You can click through the pages to see every last one of them, or click the links on the right to narrow it down.'
)
);
?>