Plugin Directory

Changeset 2332943


Ignore:
Timestamp:
06/30/2020 03:24:40 PM (6 years ago)
Author:
chuhpl
Message:

Fixed date (changed from date_i18n to date) on the check out page to correct timezone.
Changed page to 1 when you activate a filter on the posts page.

Location:
lendingq
Files:
23 added
3 edited

Legend:

Unmodified
Added
Removed
  • lendingq/trunk/lendingq.php

    r2327478 r2332943  
    44Plugin URI: https://wordpress.org/plugins/lendingq/
    55Description: A simple system to manage the lending of items (like hotspots) with an integrated waiting list.
    6 Version: 0.96.1
     6Version: 0.96.2
    77License: GPLv2 or later
    88Text Domain: lendingq
     
    9595           
    9696            add_action( 'plugins_loaded',                   [ $this, 'lendingq_load_plugin_textdomain'] );
     97
    9798        } // END function __construct()
    98         //
     99       
     100        //     
    99101        static function lendingq_activate() {
    100102            add_option( "lendingq_field_card",              __( 'Library Card Number', 'lendingq' ) );
     
    258260                    $terms          = get_terms( $slug, [ 'hide_empty' => false, 'parent' => 0 ] );
    259261                    if( count($terms) > 0 ) {
    260                         echo "<select name=\"{$slug}\" id=\"{$slug}\" class=\"postform\">";
     262                        echo "<select name=\"{$slug}\" id=\"filter-by-{$slug}\" class=\"postform\">";
    261263                        echo '<option value="">'. sprintf( __( 'Show All %s', 'lendingq' ), $tax_name ) .'</option>';
    262264                        foreach ($terms as $term) {
     
    12431245            wp_redirect( $url );
    12441246            exit;
    1245         }       
     1247        }
     1248        function handle_post_check_hold_new( $post_id, $post ) {
     1249            // don't do on autosave or when new posts are first created
     1250            if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_status == 'auto-draft' ) return $post_id;
     1251           
     1252            preme( $_REQUEST );
     1253            preme( get_terms( [ 'taxonomy' => 'lendingq_location', 'hide_empty' => false ] ) );
     1254            // get current post values
     1255           
     1256            #preme( array_filter( $_REQUEST['tax_input']['lendingq_location'] ) );
     1257            #preme( array_filter( $_REQUEST['tax_input']['lendingq_item_type'] ) );
     1258           
     1259            // get new meta
     1260            $meta_arr = [ 'card', 'contact', 'email', 'lendingq_item_type', 'lendingq_location','name', 'notes', 'phone', 'staff', 'verified' ];
     1261           
     1262            foreach( $meta_arr as $key ) {
     1263                switch( $key ) {
     1264                    case ( $key == 'lendingq_item_type' || $key == 'lendingq_location' ):
     1265                       
     1266                        $tax_id = ( empty( $_REQUEST['tax_input'][$key] ) ) ? null : sanitize_text_field( current( array_filter( $_REQUEST['tax_input'][$key] ) ) );
     1267                       
     1268                        $term = get_term( $tax_id, $key );
     1269                        $new_post_meta[$key] = $term->slug;
     1270                       
     1271                        break;
     1272                    case 'notes':
     1273                        $new_post_meta[$key] = ( empty( $_REQUEST[$key] ) ) ? null : sanitize_textarea_field( $_REQUEST[$key] );
     1274                        break;
     1275                    default:
     1276                        $new_post_meta[$key] = ( empty( $_REQUEST[$key] ) ) ? null : sanitize_text_field( $_REQUEST[$key] );
     1277                        break;
     1278                } // END switch( $key )
     1279            } // END foreach( $meta_arr as $key )
     1280           
     1281           
     1282            // get old meta
     1283            $post_meta = get_post_meta( $post_id );
     1284            $meta_arr = [ 'card', 'contact', 'email', 'lendingq_item_type','lendingq_location', 'name', 'notes', 'phone', 'staff', 'verified' ];
     1285            foreach( $meta_arr as $key ) {
     1286                switch( $key ) {
     1287                    case ( $key == 'lendingq_item_type' || $key == 'lendingq_location' ):
     1288                       
     1289                        if( empty( $terms = get_the_terms( $post, $key ) ) ) {
     1290                            $old_post_meta[$key] = null;
     1291                        } else {
     1292                            $old_post_meta[$key] = current( $terms )->slug;
     1293                        }
     1294                        break;
     1295                    default:
     1296                        $old_post_meta[$key] = ( empty( $post_meta[$key] ) ) ? null : current( $post_meta[$key] );
     1297                        break;
     1298                } // END switch( $key )
     1299            } // END foreach( $meta_arr as $key )
     1300
     1301            #preme( get_the_terms( $post, 'lendingq_location' ) );
     1302            #preme( get_the_terms( $post, 'lendingq_item_type' ) );
     1303            echo "<h2>Old</h2>";
     1304            preme( $old_post_meta );
     1305            echo "<h2>New</h2>";
     1306            preme( $new_post_meta );
     1307                       
     1308            #preme( $post );
     1309            #preme( $_REQUEST );
     1310           
     1311           
     1312            die();
     1313
     1314            /*
     1315            $post->tax_input['lendingq_location'];
     1316            $post->tax_input['lendingq_item_type'];
     1317            $post->name
     1318            $post->card
     1319            $post->verified
     1320            $post->phone
     1321            $post->email
     1322            $post->contact
     1323            $post->staff
     1324            $post->notes
     1325            */
     1326   
     1327            global $typenow, $old_date;
     1328            #$this->update_locationq_meta_hold( $post_ID );
     1329            if( $typenow !== 'lendingq_hold' ) {
     1330                return $post;
     1331            } // END if( $typenow !== 'lendingq_hold' )
     1332            if( $post->post_status == 'trash' or $post->post_status == 'auto-draft' ) {
     1333                return $post;
     1334            } // END if( $post->post_status == 'trash' or $post->post_status == 'auto-draft' )
     1335            $error = 0;
     1336            $post_meta  = get_post_meta( sanitize_text_field( $post_ID ) );
     1337            /* -------------------------------------------------- */
     1338            /* CHECK FIELDS */
     1339            if( empty( get_the_terms( $post, 'lendingq_item_type' ) ) ) {
     1340                $error = $error | 2**LENDINGQ_ITEM_TYPE_NONE;
     1341            }
     1342            if( empty( get_the_terms( $post, 'lendingq_location' ) ) ) {
     1343                $error = $error | 2**LENDINGQ_LOCATION_NONE;
     1344            }
     1345            // No name
     1346            $field_name = get_option( 'lendingq_field_name' );
     1347            if( empty( $post_meta['name'][0] ) ) {
     1348               $error = $error | 2**LENDINGQ_NAME_NONE;
     1349            }
     1350            $field_name     = get_option( 'lendingq_field_card' );
     1351            $card_regex     = get_option( 'lendingq_field_card_regex' );
     1352            $phone_regex    = get_option( 'lendingq_field_phone_regex' );
     1353            // No card           
     1354            if( empty( $post_meta['card'][0] ) ) {
     1355                $error = $error | 2**LENDINGQ_CARD_NONE;
     1356            } elseif( !empty( $card_regex ) ) {
     1357                // Bad regex
     1358                if( !preg_match( $card_regex, $post_meta['card'][0] ) ) {
     1359                    $error = $error | 2**LENDINGQ_CARD_INVALID;
     1360                } // END if( !preg_match( $card_regex, $post_meta['card'][0] ) )
     1361            }
     1362            // Not verified
     1363            if( empty( $post_meta['verified'][0] ) ) {
     1364                $error = $error | 2**LENDINGQ_VERIFIED_NONE;
     1365            }
     1366            // No contact
     1367            if( !isset( $post_meta['contact'][0] ) ) {
     1368                $post_meta['contact'][0] = null;
     1369            }
     1370            // Contact selected, is that field filled out and valid?
     1371            switch( $post_meta['contact'][0] ) {
     1372                case 'email':
     1373                    if( empty( $post_meta['email'][0] ) ) {
     1374                        $error = $error | 2**LENDINGQ_EMAIL_NONE;
     1375                    } elseif( !is_email( $post_meta['email'][0] ) ) {
     1376                        $error = $error | 2**LENDINGQ_EMAIL_INVALID;
     1377                    }
     1378                    break;
     1379                case 'phone':
     1380                    if( empty( $post_meta['phone'][0] ) ) {
     1381                        $error = $error | 2**LENDINGQ_PHONE_NONE;
     1382                    } elseif( !preg_match( $phone_regex, $post_meta['phone'][0] ) ) {
     1383                        $error = $error | 2**LENDINGQ_PHONE_INVALID;
     1384                    }
     1385                    break;
     1386                default:
     1387                    $error = $error | 2**LENDINGQ_CONTACT_NONE;
     1388                    break;               
     1389            }
     1390            // No staff name
     1391            if( empty( $post_meta['staff'][0] ) ) {
     1392                $error = $error | 2**LENDINGQ_STAFF_NONE;
     1393            }
     1394            // if not approved yet, no date has been set.
     1395            if( !empty( $post_meta['approved'] ) ) {
     1396                // date on form
     1397                if( empty( $post_meta['form_date'][0] ) ) {
     1398                    $error = $error | 2**LENDINGQ_DATE_NONE;
     1399                } else {
     1400                    $d = DateTime::createFromFormat( get_option('date_format'), $post_meta['form_date'][0] );
     1401                    if( false == ( $d && $d->format( get_option('date_format') ) == $post_meta['form_date'][0] ) ) {
     1402                        $error = $error | 2**LENDINGQ_DATE_INVALID;
     1403                    }
     1404                }
     1405                // time on form
     1406                if( empty( $post_meta['form_time'][0] ) ) {
     1407                    $error = $error | 2**LENDINGQ_TIME_NONE;
     1408                } else {
     1409                    $t = DateTime::createFromFormat( get_option('time_format'), $post_meta['form_time'][0] );
     1410                    if( false == ( $t && $t->format( get_option('time_format') ) == $post_meta['form_time'][0] ) ) {
     1411                        $error = $error | 2**LENDINGQ_TIME_INVALID;
     1412                    }
     1413                }
     1414            }
     1415            // Fix title if name or card are empty
     1416            if( empty( $post_meta['name'][0] ) or empty( $post_meta['card'][0] ) ) {
     1417                $title = __( 'Draft - unfinished', 'lendingq' );
     1418            } else {
     1419                $title = sanitize_text_field( $post_meta['name'][0] ) . ' - ' . sanitize_text_field( $post_meta['card'][0] );               
     1420            }
     1421            if( !empty( $error ) ) {
     1422                #$status = 'draft';
     1423                update_post_meta( $post_ID, 'error', $error );
     1424            } else {
     1425                $status = 'waiting_list';
     1426                if( empty( $post_meta['approved'] ) ) {
     1427                    $current_time = current_time( 'timestamp', false );
     1428                    $form_date      = date_i18n( get_option('date_format'), $current_time );
     1429                    $form_time      = date_i18n( get_option('time_format'), $current_time );
     1430                    update_post_meta( $post_ID, 'form_date', $form_date );
     1431                    update_post_meta( $post_ID, 'form_time', $form_time );
     1432                    update_post_meta( $post_ID, 'approved', $current_time );
     1433                } else {
     1434                    $current_time = strtotime( $post_meta['form_date'][0] . ' ' . $post_meta['form_time'][0] );
     1435                }
     1436                update_post_meta( $post_ID, 'waiting_date', $current_time );
     1437                update_post_meta( $post_ID, 'error', null );
     1438            }
     1439            $post->post_status    = $status;
     1440            $post->post_title     = $title;
     1441            $post_meta  = get_post_meta( sanitize_text_field( $post_ID ) );
     1442            if( empty( $error ) ) {
     1443                add_action( 'redirect_post_location', function( $location ) {
     1444                    global $post_ID;
     1445                    $location = add_query_arg( 'message', 13, get_edit_post_link( $post_ID, 'url' ) );
     1446                    return $location;
     1447                });
     1448                remove_action( 'save_post_lendingq_hold', [ $this, 'handle_post_check_hold' ] );
     1449                wp_update_post( $post );
     1450                add_action( 'save_post_lendingq_hold', [ $this, 'handle_post_check_hold' ], 10, 2 );
     1451                $url = add_query_arg( [ 'post_type' => 'lendingq_hold', 'page' => 'lendingq_check_out_list', 'message' => 'added_hold' ], admin_url( 'edit.php' ) );
     1452                wp_redirect( $url );
     1453                exit;
     1454            } else {
     1455                add_action( 'redirect_post_location', function ( $location ) {
     1456                    global $post_ID;
     1457                    $location = add_query_arg( 'message', 12, get_edit_post_link( $post_ID, 'url' ) );
     1458                    return $location;
     1459                });
     1460            }
     1461            remove_action( 'save_post_lendingq_hold', [ $this, 'handle_post_check_hold' ] );
     1462            wp_update_post( $post );
     1463            add_action( 'save_post_lendingq_hold', [ $this, 'handle_post_check_hold' ], 10, 2 );
     1464            return $post;
     1465        } // END function handle_post_check_hold( $new_status, $old_status, $post )
     1466       
    12461467        function handle_post_check_hold( $post_ID, $post ) {
    12471468            global $typenow, $old_date;
     
    21892410            }
    21902411       
    2191        
     2412            $query->set('paged', 1 );       
     2413           
    21922414            $orderby = $query->get( 'orderby');
     2415           
    21932416            if( 'waiting_date' == $orderby ) {
    21942417                $query->set('meta_key','waiting_date');
  • lendingq/trunk/readme.txt

    r2327478 r2332943  
    3333
    3434== Changelog ==
     35= 0.96.2 =
     36* Fixed date (changed from date_i18n to date) on the check out page to correct timezone.
     37* Changed page to 1 when you activate a filter on the posts page.
     38
    3539= 0.96.1 =
    36 * Added contact info to the lend array for the check in page.
    37 * Added contact info display on the check in page to show any contact data if it exists.
    38 * Fixed the check in page to work in PHP 5 (array_column wasn't working with an array of post objects) so it doesn't always show as empty.
     40* Fixed the check in page to work in PHP 5 (array_column wasn't working with an array of post objects)
    3941
    4042= 0.96 =
  • lendingq/trunk/template_check_out_form.php

    r2327478 r2332943  
    4747                <tr>
    4848                    <th scope="row"><?php _e( 'Contact date', 'lendingq'); ?></th>
    49                     <td><?php echo date_i18n( get_option( 'date_format' ), $lend['contact_date'] ) . ' - ' . date_i18n( get_option( 'time_format' ), $lend['contact_date'] ); ?></td>
     49                    <td><?php echo date( get_option( 'date_format' ), $lend['contact_date'] ) . ' - ' . date( 'g:i a', $lend['contact_date'] ); ?></td>
    5050                </tr>
    5151                <tr>
Note: See TracChangeset for help on using the changeset viewer.