plugin_URL = WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__)); $this->plugin_dir_path = plugin_dir_url(__FILE__); $this->textarea_placeholder = '291,John\n224,Peter\n221,Paul'; $this->title_placeholder = 'metadata_title'; // process import ## add_action ( 'admin_init', array ( $this, 'process' ), 1 ); // menu page ## add_action( 'admin_menu', array( $this, 'admin_menu' ) ); // build admin menu ## // a pinch of scripts and styles ## add_action( 'admin_footer', array( $this, 'scripts_and_styles' ), 100000 ); } } /* * configure DB and update init option * * @since 0.1 */ static public function register_activation_hook() { $q_add_user_metadata_option = array( 'configured' => true ); // init running, so update configuration flag ## add_option( 'q-add-user-metadata', $q_add_user_metadata_option, '', true ); } /* * function to unset configured flag, run on plugin deactivation * * @since 0.1 */ static public function register_deactivation_hook() { // deconfigure plugin ## delete_option('q-add-user-metadata'); } /* * Load Plugin Text Domain ## * * @since 0.1 */ static public function load_plugin_textdomain() { load_plugin_textdomain( 'q-add-user-metadata', false, basename( dirname( __FILE__ ) ) . '/languages' ); } /* * Create API admin menu ## * * @since 0.1 */ public function admin_menu() { add_users_page( __( 'Add Metadata', 'q-add-user-metadata' ), __( 'Add Metadata', 'q-add-user-metadata' ), 'list_users', 'add-user-metadata', array( $this, 'admin_page' ) ); } /** * Display admin page for importing users * * @since 0.1 */ public function admin_page() { // check the capabilities of the user ## // http://codex.wordpress.org/Roles_and_Capabilities#edit_users ## if ( !current_user_can( 'list_users' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); } // page title ## ?>
' . __( 'Opps! Something went wrong...', 'q-add-user-metadata' ) . '
{$row}
"; } } // debug posted options ## if ( $q_add_user_metadata_option && isset( $q_add_user_metadata_option['data'] ) ) { echo "{$key} = {$value}
"; } } // unset debug & data ## unset ( $q_add_user_metadata_option['debug'] ); unset ( $q_add_user_metadata_option['data'] ); // update option ## update_option( 'q-add-user-metadata', $q_add_user_metadata_option ); ?> sanitize( $_POST['key'], 'key' ); } else { // stop ## $good_to_go = false; // update notification $q_add_user_metadata_option['notification'] = __('Metadata Key Empty!','q-add-user-metadata'); } // were the "values" passed ## if ( isset( $_POST['values'] ) && $_POST['values'] != '' ) { // break values into array ## $values = $this->csvstring_to_array( $_POST['values'] ); // add to array ## $process['values'] = $values; if ( !is_array($process['values']) ) { // stop ## $good_to_go = false; } } else { // stop ## $good_to_go = false; // update notification $q_add_user_metadata_option['notification'] = __('Metadata Values Empty!','q-add-user-metadata'); } // was the "duplicates" passed ## if ( isset( $_POST['duplicates'] ) && $_POST['duplicates'] == 'skip' ) { $process['duplicates'] = 'skip'; } else { $process['duplicates'] = 'allow'; } // was the "debug" passed ## if ( isset( $_POST['debug'] ) && $_POST['debug'] == 'show' ) { $process['debug'] = 'show'; } else { $process['debug'] = 'hide'; } // let's process!! ## if ( $good_to_go === true ) { // so - let's do it! ## $debug = array(); foreach( $process['values'] as $row ) { // get user ID ## $user_id = $row[0]; // get the user from the ID ## $user = get_user_by( 'id', $user_id ); if ( $user === false ) { // save for debug ## $debug[] = sprintf( __( "%s This user does not exist, so I'm skipping this one.", "q-add-user-metadata" ) , "( $user_id ) -" ); // and skip on ## continue; } // get metadata value ## $value = $row[1]; // default to ok ## $check = false; // check for duplicate values ## if ( $process['duplicates'] == 'skip' ) { // check if key exists ## $exists = metadata_exists( 'user', $user_id, $process['key'] ); // not found ## if ( $exists === true ) { // check for value ## $exists_value = get_user_meta( $user_id, $process['key'], true ); if ( empty( $exists_value ) || $exists_value == '' ) { // save for debug ## #$debug[] = "( {$user_id} ) {$user->user_login} - key {$process['key']} existed, but was empty - so I updated the value to {$value}"; $debug[] = sprintf( __( "%s Key %s existed, but was empty - so I updated the value to %s", "q-add-user-metadata" ) ,( "( $user_id ) {$user->user_login} -") ,( "{$process['key']}") ,( "{$value}") ); // update the existin user meta ## update_user_meta( $user_id, $process['key'], $value ); // and skip on ## continue; } else { // debug it ## #$debug[] = "( {$user_id} ) {$user->user_login} - key {$process['key']} already has the value {$exists_value} - skipping"; $debug[] = sprintf( __( "%s Key %s already has the value %s - skipping", "q-add-user-metadata" ) ,( "( $user_id ) {$user->user_login} -") ,( "{$process['key']}") ,( "{$exists_value}") ); // and skip on ## continue; } } } // save for debug ## #$debug[] = "( {$user_id} ) {$user->user_login} - added the key {$process['key']} with the value {$value}"; $debug[] = sprintf( __( "%s Added the key %s with the value %s", "q-add-user-metadata" ) ,( "( $user_id ) {$user->user_login} -") ,( "{$process['key']}") ,( "{$value}") ); // finally - add the user meta !! ## add_user_meta( $user_id, $process['key'], $value ); } // quick test ## if ( $process['debug'] === 'show' ) { // save debug lines ## $q_add_user_metadata_option['debug'] = $debug; // clean up the debug values ## unset ( $process['values'] ); // save processed data ## $q_add_user_metadata_option['data'] = $process; } // update notification #$q_add_user_metadata_option['notification'] = "Processed ".count($debug)." Metadata Entries."; $q_add_user_metadata_option['notification'] = sprintf( __( "Processed %d Metadata Entries.", "q-add-user-metadata" ) ,count($debug) ); } // nonce failed ## } else { // wrong ## $q_add_user_metadata_option['notification'] = __('Add Metadata Failed!','q-add-user-metadata'); } // update option ## update_option( 'q-add-user-metadata', $q_add_user_metadata_option ); } // trigger notice ## add_action( 'admin_notices', array ( $this, 'admin_notice' ), 1 ); } /* * Convert CSV strong to array * * @since 0.1 */ public function csvstring_to_array( $string ) { $lines = explode( "\n", $string ); $array = array(); foreach ($lines as $line) { $array[] = str_getcsv( $this->sanitize( $line ) ); } return $array; } /* * Add Admin Notice ## * * @since 0.1 */ public function admin_notice() { $q_add_user_metadata_option = get_option( 'q-add-user-metadata', false ); if ( $q_add_user_metadata_option && isset( $q_add_user_metadata_option['notification'] ) ) { ?>