Post select options.

@since 160524 Post query utils.

@param array $args Behavioral args.

@return string Select options.
518171
public function selectOptions(array $args = []): string
    {
        // Establish args.

        $default_args = [
            // Unique.
            'allow_empty'              => true,
            'allow_arbitrary'          => true,
            'option_child_indent_char' => '-',
            'option_formatter'         => null,
            'current_post_ids'         => null,

            // Used by {@link all()}.
            'max'         => 1000,
            'fail_on_max' => true,

            // Used by {@link total()}.
            // Used by {@link all()}.
            'for_comments_only' => false,

            'include_post_ids' => [],
            'exclude_post_ids' => [],

            'include_post_types' => !$this->Wp->is_admin
                ? get_post_types(['public' => true, 'exclude_from_search' => false])
                : get_post_types(['exclude_from_search' => false]),
            'exclude_post_types' => [],

            'include_post_statuses' => !$this->Wp->is_admin
                ? get_post_stati(['public' => true, 'exclude_from_search' => false])
                : get_post_stati(['exclude_from_search' => false]),
            'exclude_post_statuses' => [],

            'exclude_drafts'             => true,
            'exclude_revisions'          => true,
            'exclude_trash'              => true,
            'exclude_password_protected' => !$this->Wp->is_admin,
            'exclude_nav_menu_items'     => true,

            'no_cache' => false,
        ];
        $args = array_merge($default_args, $args);
        $args = array_intersect_key($args, $default_args);

        // Unique.
        $args['allow_empty']              = (bool) $args['allow_empty'];
        $args['allow_arbitrary']          = (bool) $args['allow_arbitrary'];
        $args['option_child_indent_char'] = (string) $args['option_child_indent_char'];
        $args['option_formatter']         = is_callable($args['option_formatter']) ? $args['option_formatter'] : null;
        $args['current_post_ids']         = isset($args['current_post_ids']) ? (array) $args['current_post_ids'] : null;
        $args['current_post_ids']         = $this->c::removeEmptys(array_map('intval', $args['current_post_ids']));

        // Used by {@link all()}.
        $args['max']         = max(1, (int) $args['max']);
        $args['fail_on_max'] = (bool) $args['fail_on_max'];

        // Used by {@link total()}.
        // Used by {@link all()}.
        $args['for_comments_only'] = (bool) $args['for_comments_only'];

        $args['include_post_ids'] = (array) $args['include_post_ids'];
        $args['exclude_post_ids'] = (array) $args['exclude_post_ids'];

        $args['include_post_types'] = (array) $args['include_post_types'];
        $args['exclude_post_types'] = (array) $args['exclude_post_types'];

        $args['include_post_statuses'] = (array) $args['include_post_statuses'];
        $args['exclude_post_statuses'] = (array) $args['exclude_post_statuses'];

        $args['exclude_drafts']             = (bool) $args['exclude_drafts'];
        $args['exclude_revisions']          = (bool) $args['exclude_revisions'];
        $args['exclude_trash']              = (bool) $args['exclude_trash'];
        $args['exclude_password_protected'] = (bool) $args['exclude_password_protected'];
        $args['exclude_nav_menu_items']     = (bool) $args['exclude_nav_menu_items'];

        $args['no_cache'] = (bool) $args['no_cache'];

        // Check for nothing being available (or too many).

        if (!($posts = $this->all($args))) {
            return ''; // None available.
        }
        // Initialize several working variables needed below.

        $options                 = ''; // Initialize.
        $available_post_ids      = []; // Initialize.
        $selected_post_ids       = []; // Initialize.
        $default_post_type_label = __('Post', 'wp-sharks-core');
        $default_post_title      = __('Untitled', 'wp-sharks-core');

        // Build & return all `<option>` tags.

        if ($args['allow_empty']) { // Allow `0`?
            $options = '<option value="0"></option>';
        }
        $walk = function (// Recursive parent/child walker.
            int $parent_post_id = 0,
            int $parent_depth = 0
        ) use (
            &$walk,
            &$args,
            &$posts,
            &$options,
            &$available_post_ids,
            &$selected_post_ids,
            &$default_post_type_label,
            &$default_post_title
        ) {
            foreach ($posts as $_post) { // \WP_Post objects.
                if ((int) $_post->post_parent !== $parent_post_id) {
                    continue; // Bypass this child for now.
                }
                $available_post_ids[] = (int) $_post->ID; // Record all available.

                if (isset($args['current_post_ids']) && in_array((int) $_post->ID, $args['current_post_ids'], true)) {
                    $selected_post_ids[$_post->ID] = (int) $_post->ID; // Flag selected post ID.
                }
                $_post_type_object = get_post_type_object($_post->post_type); // Anticipate a possible failure.
                $_post_type_label  = !empty($_post_type_object->labels->singular_name) ? $_post_type_object->labels->singular_name : $default_post_type_label;

                $_post_title            = $_post->post_title ?: $default_post_title;
                $_post_date             = $this->s::dateI18nUtc('M jS, Y', strtotime($_post->post_date_gmt));
                $_post_id_selected_attr = isset($selected_post_ids[$_post->ID]) ? ' selected' : '';

                // Format `<option>` tag w/ a custom formatter?

                if ($args['option_formatter']) {
                    $options .= $args['option_formatter']($_post, [
                            'parent_post_id'        => $parent_post_id,
                            'parent_depth'          => $parent_depth,
                            'post_type_object'      => $_post_type_object,
                            'post_type_label'       => $_post_type_label,
                            'post_title'            => $_post_title,
                            'post_date'             => $_post_date,
                            'post_id_selected_attr' => $_post_id_selected_attr,
                        ], $args); // ↑ This allows for a custom option formatter.
                        // The formatter must always return an `<option></option>` tag.

                // Else format the `<option>` tag using a default behavior.
                } elseif ($this->Wp->is_admin) { // Slightly different format in admin area.
                    $options .= '<option value="'.esc_attr($_post->ID).'"'.$_post_id_selected_attr.'>'.
                                    ($parent_depth > 0 ? str_repeat('&nbsp;', $parent_depth).$args['option_child_indent_char'].' ' : '').
                                    esc_html($_post_type_label.' #'.$_post->ID.': '.$_post_title).
                                '</option>';
                } else { // Front-end display should be friendlier in some ways.
                    $options .= '<option value="'.esc_attr($_post->ID).'"'.$_post_id_selected_attr.'>'.
                                    ($parent_depth > 0 ? str_repeat('&nbsp;', $parent_depth).$args['option_child_indent_char'].' ' : '').
                                    esc_html($_post_date.' — '.$_post_title).
                                '</option>';
                }
                $walk((int) $_post->ID, $parent_depth + 1); // Any children this term has.
                //
            } // unset($_post, $_post_type_object, $_post_type_label, $_post_title, $_post_date, $_post_id_selected_attr); // Housekeeping.
        };
        $walk(0, 0); // Start walking/building the parent » child `<option>` tags.

        if ($args['allow_arbitrary'] && $args['current_post_ids']) { // Allow arbitrary select `<option>`s?
            foreach (array_diff($args['current_post_ids'], $available_post_ids) as $_arbitrary_post_id) {
                $options .= '<option value="'.esc_attr($_arbitrary_post_id).'" selected>'.
                                esc_html($default_post_type_label.' #'.$_arbitrary_post_id).
                            '</option>';
            } // unset($_arbitrary_post_id); // Housekeeping.
        }
        return $options; // HTML markup.
    }
1150379
public function execute(array $args, array $options = [])
    {
        if (!isset($args[0])) {
            throw new \InvalidArgumentException('arg action must be input');
        }
        $action = explode('::', $args[0]);
        if (!class_exists($action[0])) {
            throw new \InvalidArgumentException('action not not found!');
        }

        if (isset($args[1])) {
            $frequency = abs(intval($args[1]));
        } else {
            $frequency = null;
        }
        ProcessManage::addTask($action, $frequency);
    }
1143234
private function parseLongOption($arg)
    {

        $longOptions  = $this->longOptions;

        // If our program does not accept long options
        // ignore current keyword and increment index
        if (empty($longOptions)) {
            $this->optind++;
            return null;
        }

        $key = $this->getLongOptionKey($arg);

        // Init return value to NULL (invalid option)
        $option = null;

        // Search if the option is in the list of valid long options
        foreach ($longOptions as $opt) {
            // Transform in array, this allow string-only declarations in options array
            if (!is_array($opt)) {
                $opt = array($opt);
            }

            if ((string)$opt[0] === $key) {
                // Match found, set return option name
                $option = $key;
                
                // If 1-char equiv is present, it overrides long name
                if (isset($opt[2]) && strlen((string)$opt[2]) == 1) {
                    $option = $opt[2];
                }

                // If option should have a parameter
                if (isset($opt[1]) && true === $opt[1]) {
                    if (false !== ($eqPos = strpos($arg, '='))) {
                        // Parsing equal format (--key=value): parameter value is the string after '='
                        $this->optarg = substr($arg, $eqPos + 1);
                        
                        // Index is updated by 1 step only
                        $this->optind++;
                        return $option;

                    }

                    // Parameter should be in the command line in the next position
                    // Test if arg is a real arg or another option (contains - or --)
                    if ($this->optind < $this->argc -1) {
                        $optarg = $this->argv[$this->optind + 1];
                        if (!($this->isLongOption($optarg)) && !($this->isShortOption($optarg))) {
                            // Option value is ok, set it and update index by 2 steps
                            $this->optarg = $optarg;
                            $this->optind += 2;
                            return $option;
                        }

                    }

                    // Option value missing, set it to FALSE and update index by 1 step only
                    $this->optarg = false;
                    $this->optind++;
                    return $option;
                }

                $this->optarg = true;
                $this->optind++;
                return $option;
            }
        }

         // At the end of the loop $option can be NULL or a string value
        return $option;
    }
842094
public static function select($params = array()) {

        if( !is_array($params) )
        {
            throw new \InvalidArgumentException("Arguments is not a Array". print_r($params, true));
        }

        $o = "<select";
        $o .= (isset($params['id'])) ? " id='{$params['id']}'" : '';
        $o .= (isset($params['name'])) ? " name='{$params['name']}'" : '';
        $o .= (isset($params['class'])) ? " class='{$params['class']}'" : '';
        $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : '';
        $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : '';
        $o .= (isset($params['required'])) ? " required='required'" : '';
        $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : '';
        $o .= (isset($params['style'])) ? " style='{$params['style']}'" : '';
        $o .= ">\n";
        $o .= "<option value=''>Select</option>\n";
        if (isset($params['data']) && is_array($params['data'])) {
            foreach ($params['data'] as $k => $v) {
                if (isset($params['value']) && $params['value'] == $k) {
                    $o .= "<option value='{$k}' selected='selected'>{$v}</option>\n";
                } else {
                    $o .= "<option value='{$k}'>{$v}</option>\n";
                }
            }
            throw new \InvalidArgumentException("Arguments is not valid ". print_r($params, true));

        }
        $o .= "</select>\n";
        return $o;
    }
1314902
public function parse_query_args( $args ) {
		$query_args = [
			'frontend' => $args,
			'backend'  => $args,
		];

		if ( isset( $args['frontend'] ) || isset( $args['backend'] ) ) {
			foreach ( [ 'frontend', 'backend' ] as $query_type ) {
				$query_args[ $query_type ] = isset( $args[ $query_type ] )
					? $args[ $query_type ]
					: [];
			}
		}

		return $query_args;
	}
440443
public function select($options = [])
    {
        $source = [];

        foreach ($options as $key => $value) {
            $source[] = [
                'value' => $key,
                'text' => $value,
            ];
        }

        $this->addOptions(['source' => $source]);
    }
817044
static function renderCustomSelect( $args ) {
		$defaults = [
			'id' => null,
			'name' => null,
			'value' => '',
			'choices' => [],
			'multiple' => false,
			'disabled' => false,
		];

		$args = wp_parse_args( $args, $defaults );

		$is_custom = false;
		if ( ! array_key_exists( $args['value'], $args['choices'] ) ) {
			$is_custom = true;
		}
		$options = '';
		foreach ( $args['choices'] as $key => $label ) {
			$options .= sprintf(
				'<option value="%s" %s>%s</option>',
				$key,
				( empty( $key ) && $is_custom ) ? ' selected' : selected( $key, $args['value'], false ),
				$label
			);
		}
		printf(
			'<select name="%s" id="%s" %s%s>%s</select><br />',
			$args['name'],
			$args['id'],
			( $args['multiple'] ) ? ' multiple' : '',
			( ! empty( $args['disabled'] ) ) ? ' disabled' : '',
			$options
		);
	}
1236845
public function input($arg,...$args){
        /** $args != null */
        if($args) {
            if(is_array($args[0])) {
                //$this->input('get',['name','pass']);
                $args = [$arg,$args[0]];
            }
            else {
                //$this->input('name','pass');
                array_unshift($args,$arg);
                $args = [$this->method,$args];
            }
        }
        else {
            /** $args == null */
            //$this->input('name');
            //$this->input(['name','pass']);
            $args = [$this->method,$arg];
        }

        $input = call_user_func_array([$this,'form'],$args);

        if(is_array($input) === false) {
            return null;
        }

        if(count($input) == 1) {
            return current($input);
        }

        return array_values($input);
    }
1033763
public function form(string $slug, array $args = []): Classes\SCore\PostMetaBoxForm
    {
        return $this->App->Di->get(Classes\SCore\PostMetaBoxForm::class, compact('slug', 'args'));
    }
1003523
public function fetchSingle(...$args)
    { 
        $args[] = QB::limit(1);
        $select = $this->select($args);
        return $select->fetch();
    }
437474
public function execute(array $args = [], array $options = [])
    {
        /** @var CommandInterface[] $commands */
        $commands = $this->getConfig()->get('commands');

        $all = isset($options['all']) && true === $options['all'];

        $selectedCommands = [];
        foreach ($commands as $n => $c) {
            $hidden = false;
            $realName = $n;
            if ('_' === $n{0}) {
                if (!$all) {
                    continue;
                }
                $hidden = true;
                $n = substr($n, 1);
            }
            $selectedCommands[$n] = ['hidden' => $hidden, 'realName' => $realName, 'command' => $c];
        }

        ksort($selectedCommands);

        $user = $this->getConfig()->get('user_name');

        if ($user) {
            $this->writeln('<comment>DEV Console command</comment> (Happy hacking %s !)', $user);
        } else {
            $this->writeln('<comment>DEV Console command</comment>');
        }
        $this->writeln();
        $this->writeln('%d commands:', count($selectedCommands));

        $max = 0;

        foreach ($selectedCommands as $commandName => $def) {
            $len = strlen($commandName);
            $max = $max < $len ? $len : $max;
        }

        $lastLetter = null;

        foreach ($selectedCommands as $commandName => $def) {
            $letter = $commandName{0};
            if ($lastLetter !== $letter) {
                $this->writeln();
            }
            /** @var CommandInterface $command */
            $command = $def['command'];
            $command->setConfig($this->getConfig());
            $this->writeln("  <".($def['hidden'] ? 'comment' : 'info').">%-".$max."s</".($def['hidden'] ? 'comment' : 'info').">  <comment>%s</comment>", $commandName, $command->getDescription());
            $lastLetter = $letter;
        }

        $this->writeln();
    }
124337
public function execute($js, $args = [])
    {
        $params = ['script' => $js, 'args' => $args];
        $result = $this->getDriver()->curl(
            $this->getDriver()->factoryCommand('execute', WebDriver_Command::METHOD_POST, $params)
        );
        return isset($result['value'])?$result['value']:false;
    }
55560
public static function get_posts( $args ) {
		if ( is_string( $args ) ) {
			$args = add_query_arg(
				[
					'suppress_filters' => false,
				]
			);
		} elseif ( is_array( $args ) && ! isset( $args['suppress_filters'] ) ) {
			$args['suppress_filters'] = false;
		}

		// Get the posts.
		// TODO: WordPress.VIP.RestrictedFunctions.get_posts_get_posts.
		$posts = get_posts( $args );

		// Properly format the array.
		$items = [];
		foreach ( $posts as $post ) {
			$items[ $post->ID ] = $post->post_title;
		}
		wp_reset_postdata();

		return $items;
	}
1233680
private function parse($args)
    {
        foreach ($args as $argument) {
            $this->arguments->push($argument);
            if ($this->detectFlags($argument)) {
                continue;
            }
            if ($this->detectOptions($argument)) {
                continue;
            }
            if ($this->detectConcatFlag($argument)) {
                continue;
            }
            $this->commands->push($argument);
        }
    }
1339333
public function selectArray($q, $args = array())
    {
        $this->exec($q, $args);
        return $this->_sth ? $this->_sth->fetchAll(\PDO::FETCH_ASSOC) : false;
    }
565304
public static function addColorOption(array $args, $colorOption)
    {
        if (!$colorOption
            || in_array($colorOption, $args)
            || !preg_match('/^--([a-z]+$)|(^--[a-z]+=)/', $colorOption, $matches)) {
            return $args;
        }

        if (isset($matches[2])) {
            // Handle --color(s)= options
            if (false !== ($index = array_search($matches[2].'auto', $args))) {
                $args[$index] = $colorOption;
                return $args;
            } elseif (preg_grep('/^'.$matches[2].'/', $args)) {
                return $args;
            }
        } elseif (in_array('--no-'.$matches[1], $args)) {
            return $args;
        }

        if (false !== ($index = array_search('--', $args))) {
            // Position option before double-dash delimiter
            array_splice($args, $index, 0, $colorOption);
        } else {
            $args[] = $colorOption;
        }

        return $args;
    }
266660
private function _get_value($args){
    if ( $this->id ){
      $var =& $this->data;
      foreach ( $args as $a ){
        if ( !isset($var[$a]) ){
          return null;
        }
        $var =& $var[$a];
      }
      return $var;
    }
  }