santiago-c
6/3/2019 - 8:34 PM

cityam settings update something


/********************************************************************************************************************************************************/
/**
 * Custom Settings button to trigger update the users Bios
 * https://developer.wordpress.org/reference/functions/wp_insert_post/
 */
/********************************************************************************************************************************************************/
 
 
/*
// Create an options page for one button that will trigger the update
*/
class MySettingsPage
{
    /**
     * Holds the values to be used in the fields callbacks
     */
    // private $options;
 
    /**
     * Start up
     */
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
    }
 
    /**
     * Add options page
     */
    public function add_plugin_page()
    {
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin', 
            'My Update Settings', 
            'create_users', //https://codex.wordpress.org/Roles_and_Capabilities#Capabilities
            'my-setting-admin', 
            array( $this, 'create_admin_page' )
        );
    }
 
    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        // Set class property
        // $this->options = get_option( 'my_option_name' );
 
        ?>
        <div class="wrap">
 
          <h1>My Settings</h1>
 
          <div class="box-classes">            	
            <button>Update User Bios</button>
            <div class="msg"><!-- Ajax Request Here --></div>
						<div class="loader hidden"><div class="line"></div><div class="line"></div><div class="line"></div></div>
          </div><!-- box-classes -->
 
        </div><!-- wrap -->
 
				<style type="text/css">
					.loader { display:block; float:left; width:80px; height:80px; border-radius:50%; background-color:#1695A3; margin-left:16px; position:relative; box-sizing:border-box; background-color:transparent; }
					.loader.hidden { display:none; }
					.loader .line { width:30%; height:30%; border-radius:5px; background-color:#1695A3; position:absolute; bottom:50%; top:50%; transform:translateY(-50%); }
					.loader .line:first-child { animation:height1 0.9s ease-in infinite; left:0; }
					.loader .line:nth-child(2) { animation:height2 0.9s ease-in infinite; left:35%; }
					.loader .line:last-child { animation:height3 0.9s ease-out infinite; right:0; }
					@keyframes height1 {
					  0% { height:30%; }
					  50% { height:50%; }
					  100% { height:30%; }
					}
					@keyframes height2 {
					  0% { height:50%; }
					  50% { height:30%; }
					  100% { height:50%; }
					}
					@keyframes height3 {
					  0% { height:20%; }
					  50% { height:70%; }
					  100% { height:20%; }
					}	
				</style>
 
        <?php
    }
}
 
$current_user = wp_get_current_user();
if( $current_user && isset($current_user->user_login) && 'publisher' == $current_user->user_login && is_admin() ) {
 
		// Options Page
    $my_settings_page = new MySettingsPage();
	
 
    // CLASSES
		add_action( 'admin_footer', 'update_classes_action_javascript' ); // Write our JS below here
		function update_classes_action_javascript() { ?>
			<script type="text/javascript" >
			jQuery(document).ready(function($) {
 
				var data = {
					'action': 'update_classes_action',
					'whatever': 1234
				};
 
				$(".box-classes button").click(function(){
					$(".box-classes .loader").removeClass("hidden");
					// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
					jQuery.post(ajaxurl, data, function(response) {
						$(".box-classes .loader").addClass("hidden");
						if(response==""){
							$(".box-classes .msg").html("No Data");
						}else{
							var obj = JSON.parse(response);
							console.log(obj);
							$(".box-classes .msg").html("Json Data received");
						}
					});
				});
 
			});
			</script> <?php
		}
 
		add_action( 'wp_ajax_update_classes_action', 'update_classes_action' );
		function update_classes_action() {
 
			global $wpdb; // this is how you get access to the database
			$str = array();
            $success = 0;
            /*
            $posts = array(
                1023453,
                1023424,
                1023453,
                1049945,
                1023438,
                1018945,
            );

            foreach ($posts as &$post_ID) {
                if( update_post_meta( $post_ID, 'company', '1070462') == true )   // Assign company Capital to the post
                    $success++;
            }
            */
            $args = array(
                'meta_query' => array(
                    array(
                        'key' => 'sponsored_media',
                        'value' => '1',
                        'compare' => '=',
                    )
                )
            );
            $query = new WP_Query($args);            
            foreach ($query as &$post) {
            }


			$str[] = array( "Rows: ".count($users), "Failed: ".(count($users)-$success), "Success: ".$success );
			echo json_encode($str);
			wp_die(); // this is required to terminate immediately and return a proper response
 
		}//update_classes_action
 
}