idx
int64
0
60.3k
question
stringlengths
101
6.21k
target
stringlengths
7
803
11,900
protected function generateFileName ( $ table , $ prefix = null , $ suffix = null ) { if ( ! \ Schema :: connection ( $ this -> option ( 'database' ) ? $ this -> option ( 'database' ) : config ( 'database.default' ) ) -> hasTable ( $ table ) ) { throw new TableNotFoundException ( "Table $table was not found." ) ; } $ className = app ( 'iseed' ) -> generateClassName ( $ table , $ prefix , $ suffix ) ; $ seedPath = base_path ( ) . config ( 'iseed::config.path' ) ; return [ $ seedPath . '/' . $ className . '.php' , $ className . '.php' ] ; }
Generate file name to be used in test wether seed file already exist
11,901
public function generateSeed ( $ table , $ prefix = null , $ suffix = null , $ database = null , $ max = 0 , $ chunkSize = 0 , $ exclude = null , $ prerunEvent = null , $ postrunEvent = null , $ dumpAuto = true , $ indexed = true , $ orderBy = null , $ direction = 'ASC' ) { if ( ! $ database ) { $ database = config ( 'database.default' ) ; } $ this -> databaseName = $ database ; if ( ! $ this -> hasTable ( $ table ) ) { throw new TableNotFoundException ( "Table $table was not found." ) ; } $ data = $ this -> getData ( $ table , $ max , $ exclude , $ orderBy , $ direction ) ; $ dataArray = $ this -> repackSeedData ( $ data ) ; $ className = $ this -> generateClassName ( $ table , $ prefix , $ suffix ) ; $ stub = $ this -> readStubFile ( $ this -> getStubPath ( ) . '/seed.stub' ) ; $ seedPath = $ this -> getSeedPath ( ) ; $ seedsPath = $ this -> getPath ( $ className , $ seedPath ) ; $ seedContent = $ this -> populateStub ( $ className , $ stub , $ table , $ dataArray , $ chunkSize , $ prerunEvent , $ postrunEvent , $ indexed ) ; $ this -> files -> put ( $ seedsPath , $ seedContent ) ; if ( $ dumpAuto ) { $ this -> composer -> dumpAutoloads ( ) ; } return $ this -> updateDatabaseSeederRunMethod ( $ className ) !== false ; }
Generates a seed file .
11,902
public function getData ( $ table , $ max , $ exclude = null , $ orderBy = null , $ direction = 'ASC' ) { $ result = \ DB :: connection ( $ this -> databaseName ) -> table ( $ table ) ; if ( ! empty ( $ exclude ) ) { $ allColumns = \ DB :: connection ( $ this -> databaseName ) -> getSchemaBuilder ( ) -> getColumnListing ( $ table ) ; $ result = $ result -> select ( array_diff ( $ allColumns , $ exclude ) ) ; } if ( $ orderBy ) { $ result = $ result -> orderBy ( $ orderBy , $ direction ) ; } if ( $ max ) { $ result = $ result -> limit ( $ max ) ; } return $ result -> get ( ) ; }
Get the Data
11,903
public function repackSeedData ( $ data ) { if ( ! is_array ( $ data ) ) { $ data = $ data -> toArray ( ) ; } $ dataArray = array ( ) ; if ( ! empty ( $ data ) ) { foreach ( $ data as $ row ) { $ rowArray = array ( ) ; foreach ( $ row as $ columnName => $ columnValue ) { $ rowArray [ $ columnName ] = $ columnValue ; } $ dataArray [ ] = $ rowArray ; } } return $ dataArray ; }
Repacks data read from the database
11,904
public function populateStub ( $ class , $ stub , $ table , $ data , $ chunkSize = null , $ prerunEvent = null , $ postrunEvent = null , $ indexed = true ) { $ chunkSize = $ chunkSize ? : config ( 'iseed::config.chunk_size' ) ; $ inserts = '' ; $ chunks = array_chunk ( $ data , $ chunkSize ) ; foreach ( $ chunks as $ chunk ) { $ this -> addNewLines ( $ inserts ) ; $ this -> addIndent ( $ inserts , 2 ) ; $ inserts .= sprintf ( "\DB::table('%s')->insert(%s);" , $ table , $ this -> prettifyArray ( $ chunk , $ indexed ) ) ; } $ stub = str_replace ( '{{class}}' , $ class , $ stub ) ; $ prerunEventInsert = '' ; if ( $ prerunEvent ) { $ prerunEventInsert .= "\$response = Event::until(new $prerunEvent());" ; $ this -> addNewLines ( $ prerunEventInsert ) ; $ this -> addIndent ( $ prerunEventInsert , 2 ) ; $ prerunEventInsert .= 'if ($response === false) {' ; $ this -> addNewLines ( $ prerunEventInsert ) ; $ this -> addIndent ( $ prerunEventInsert , 3 ) ; $ prerunEventInsert .= 'throw new Exception("Prerun event failed, seed wasn\'t executed!");' ; $ this -> addNewLines ( $ prerunEventInsert ) ; $ this -> addIndent ( $ prerunEventInsert , 2 ) ; $ prerunEventInsert .= '}' ; } $ stub = str_replace ( '{{prerun_event}}' , $ prerunEventInsert , $ stub ) ; if ( ! is_null ( $ table ) ) { $ stub = str_replace ( '{{table}}' , $ table , $ stub ) ; } $ postrunEventInsert = '' ; if ( $ postrunEvent ) { $ postrunEventInsert .= "\$response = Event::until(new $postrunEvent());" ; $ this -> addNewLines ( $ postrunEventInsert ) ; $ this -> addIndent ( $ postrunEventInsert , 2 ) ; $ postrunEventInsert .= 'if ($response === false) {' ; $ this -> addNewLines ( $ postrunEventInsert ) ; $ this -> addIndent ( $ postrunEventInsert , 3 ) ; $ postrunEventInsert .= 'throw new Exception("Seed was executed but the postrun event failed!");' ; $ this -> addNewLines ( $ postrunEventInsert ) ; $ this -> addIndent ( $ postrunEventInsert , 2 ) ; $ postrunEventInsert .= '}' ; } $ stub = str_replace ( '{{postrun_event}}' , $ postrunEventInsert , $ stub ) ; $ stub = str_replace ( '{{insert_statements}}' , $ inserts , $ stub ) ; return $ stub ; }
Populate the place - holders in the seed stub .
11,905
protected function prettifyArray ( $ array , $ indexed = true ) { $ content = ( $ indexed ) ? var_export ( $ array , true ) : preg_replace ( "/[0-9]+ \=\>/i" , '' , var_export ( $ array , true ) ) ; $ lines = explode ( "\n" , $ content ) ; $ inString = false ; $ tabCount = 3 ; for ( $ i = 1 ; $ i < count ( $ lines ) ; $ i ++ ) { $ lines [ $ i ] = ltrim ( $ lines [ $ i ] ) ; if ( strpos ( $ lines [ $ i ] , ')' ) !== false ) { $ tabCount -- ; } if ( $ inString === false ) { for ( $ j = 0 ; $ j < $ tabCount ; $ j ++ ) { $ lines [ $ i ] = substr_replace ( $ lines [ $ i ] , $ this -> indentCharacter , 0 , 0 ) ; } } for ( $ j = 0 ; $ j < strlen ( $ lines [ $ i ] ) ; $ j ++ ) { if ( $ lines [ $ i ] [ $ j ] == '\\' ) { $ j ++ ; } else if ( $ lines [ $ i ] [ $ j ] == '\'' ) { $ inString = ! $ inString ; } } if ( strpos ( $ lines [ $ i ] , '(' ) !== false ) { $ tabCount ++ ; } } $ content = implode ( "\n" , $ lines ) ; return $ content ; }
Prettify a var_export of an array
11,906
public function cleanSection ( ) { $ databaseSeederPath = base_path ( ) . config ( 'iseed::config.path' ) . '/DatabaseSeeder.php' ; $ content = $ this -> files -> get ( $ databaseSeederPath ) ; $ content = preg_replace ( "/(\#iseed_start.+?)\#iseed_end/us" , "#iseed_start\n\t\t#iseed_end" , $ content ) ; return $ this -> files -> put ( $ databaseSeederPath , $ content ) !== false ; return false ; }
Cleans the iSeed section
11,907
public function mutate ( Node $ node ) { return new Node \ Expr \ AssignOp \ Plus ( $ node -> var , $ node -> expr , $ node -> getAttributes ( ) ) ; }
Replaces - = with + =
11,908
public static function createFromArray ( array $ mutantProcesses ) : self { $ self = new self ( ) ; foreach ( $ mutantProcesses as $ process ) { $ self -> collect ( $ process ) ; } return $ self ; }
Build a metric calculator with a sub - set of mutators
11,909
public function getCoverageRate ( ) : float { $ coveredRate = 0 ; $ coveredByTestsTotal = $ this -> totalMutantsCount - $ this -> notCoveredByTestsCount ; if ( $ this -> totalMutantsCount ) { $ coveredRate = floor ( 100 * $ coveredByTestsTotal / $ this -> totalMutantsCount ) ; } return $ coveredRate ; }
Mutation coverage percentage
11,910
private function getOuterMostArrayNode ( Node $ node ) : Node { $ outerMostArrayParent = $ node ; do { if ( $ node instanceof Node \ Expr \ Array_ ) { $ outerMostArrayParent = $ node ; } } while ( $ node = $ node -> getAttribute ( ParentConnectorVisitor :: PARENT_KEY ) ) ; return $ outerMostArrayParent ; }
If the node is part of an array this will find the outermost array . Otherwise this will return the node itself
11,911
public function start ( callable $ callback = null , array $ env = null ) : void { $ phpConfig = new PhpConfig ( ) ; $ phpConfig -> useOriginal ( ) ; parent :: start ( $ callback , $ env ?? [ ] ) ; $ phpConfig -> usePersistent ( ) ; }
Runs a PHP process with xdebug loaded
11,912
private function isInsideFunction ( Node $ node ) : bool { if ( ! $ node -> hasAttribute ( ParentConnectorVisitor :: PARENT_KEY ) ) { return false ; } $ parent = $ node -> getAttribute ( ParentConnectorVisitor :: PARENT_KEY ) ; if ( $ parent -> getAttribute ( self :: IS_INSIDE_FUNCTION_KEY ) ) { return true ; } if ( $ this -> isFunctionLikeNode ( $ parent ) ) { return true ; } return $ this -> isInsideFunction ( $ parent ) ; }
Recursively determine whether the node is inside the function
11,913
protected function initialize ( InputInterface $ input , OutputInterface $ output ) : void { parent :: initialize ( $ input , $ output ) ; $ locator = $ this -> getContainer ( ) -> get ( 'locator' ) ; if ( $ customConfigPath = $ input -> getOption ( 'configuration' ) ) { $ locator -> locate ( $ customConfigPath ) ; } else { $ this -> runConfigurationCommand ( $ locator ) ; } $ this -> consoleOutput = $ this -> getApplication ( ) -> getConsoleOutput ( ) ; $ this -> skipCoverage = \ strlen ( trim ( $ input -> getOption ( 'coverage' ) ) ) > 0 ; $ this -> eventDispatcher = $ this -> getContainer ( ) -> get ( 'dispatcher' ) ; }
Run configuration command if config does not exist
11,914
public function mutate ( Node $ node ) { return new Node \ Expr \ BinaryOp \ Minus ( $ node -> left , $ node -> right , $ node -> getAttributes ( ) ) ; }
Replaces + with -
11,915
public function mutate ( Node $ node ) { if ( $ node -> value === 0 ) { return new Node \ Scalar \ LNumber ( 1 ) ; } return new Node \ Scalar \ LNumber ( 0 ) ; }
Replaces 0 with 1 or 1 with 0
11,916
public function mutate ( Node $ node ) { if ( $ node -> value === 0.0 ) { return new Node \ Scalar \ DNumber ( 1.0 ) ; } return new Node \ Scalar \ DNumber ( 0.0 ) ; }
Replaces 0 . 0 with 1 . 0 or 1 . 0 with 0 . 0
11,917
public function mutate ( Node $ node ) { return new Node \ Expr \ AssignOp \ Minus ( $ node -> var , $ node -> expr , $ node -> getAttributes ( ) ) ; }
Replaces + = with - =
11,918
public function mutate ( Node $ node ) { return new ClassMethod ( $ node -> name , [ 'flags' => ( $ node -> flags & ~ Class_ :: MODIFIER_PUBLIC ) | Class_ :: MODIFIER_PROTECTED , 'byRef' => $ node -> returnsByRef ( ) , 'params' => $ node -> getParams ( ) , 'returnType' => $ node -> getReturnType ( ) , 'stmts' => $ node -> getStmts ( ) , ] , $ node -> getAttributes ( ) ) ; }
Replaces public function ... with protected function ...
11,919
public function mutate ( Node $ node ) { $ integerValue = $ node -> expr instanceof Node \ Expr \ UnaryMinus ? - $ node -> expr -> expr -> value : $ node -> expr -> value ; return new Node \ Stmt \ Return_ ( new Node \ Scalar \ LNumber ( - 1 * $ integerValue , $ node -> getAttributes ( ) ) ) ; }
Replaces any integer with negated integer value . Replaces - 5 with 5
11,920
public function mutate ( Node $ node ) { $ floatValue = $ node -> expr instanceof Node \ Expr \ UnaryMinus ? - $ node -> expr -> expr -> value : $ node -> expr -> value ; return new Node \ Stmt \ Return_ ( new Node \ Scalar \ DNumber ( - 1 * $ floatValue , $ node -> getAttributes ( ) ) ) ; }
Replaces any float with negated float Replaces - 33 . 4 with 33 . 4
11,921
public function mutate ( Node $ node ) { return new Node \ Expr \ Assign ( $ node -> left , $ node -> right , $ node -> getAttributes ( ) ) ; }
Replaces == with =
11,922
public function add ( $ hooks , $ callback , $ priority = 10 , $ accepted_args = 3 ) { foreach ( ( array ) $ hooks as $ hook ) { $ this -> addHookEvent ( $ hook , $ callback , $ priority , $ accepted_args ) ; } return $ this ; }
Wrapper of the add_action or add_filter functions . Allows a developer to specify a controller class or closure .
11,923
public function remove ( $ hook , $ callback = null , $ priority = 10 ) { if ( is_null ( $ callback ) ) { if ( ! $ callback = $ this -> getCallback ( $ hook ) ) { return false ; } list ( $ callback , $ priority , $ accepted_args ) = $ callback ; unset ( $ this -> hooks [ $ hook ] ) ; } $ this -> removeAction ( $ hook , $ callback , $ priority ) ; return $ this ; }
Remove a registered action or filter .
11,924
public function getCallback ( $ hook ) { if ( array_key_exists ( $ hook , $ this -> hooks ) ) { return $ this -> hooks [ $ hook ] ; } return null ; }
Return the callback registered with the hook .
11,925
protected function addHookEvent ( $ hook , $ callback , $ priority , $ accepted_args ) { if ( $ callback instanceof \ Closure || is_array ( $ callback ) ) { $ this -> addEventListener ( $ hook , $ callback , $ priority , $ accepted_args ) ; } elseif ( is_string ( $ callback ) ) { if ( false !== strpos ( $ callback , '@' ) || class_exists ( $ callback ) ) { $ callback = $ this -> addClassEvent ( $ hook , $ callback , $ priority , $ accepted_args ) ; } else { $ this -> addEventListener ( $ hook , $ callback , $ priority , $ accepted_args ) ; } } return $ callback ; }
Add an event for the specified hook .
11,926
protected function addClassEvent ( $ hook , $ class , $ priority , $ accepted_args ) { $ callback = $ this -> buildClassEventCallback ( $ class , $ hook ) ; $ this -> addEventListener ( $ hook , $ callback , $ priority , $ accepted_args ) ; return $ callback ; }
Prepare the hook callback for use in a class method .
11,927
protected function buildClassEventCallback ( $ class , $ hook ) { list ( $ class , $ method ) = $ this -> parseClassEvent ( $ class , $ hook ) ; $ instance = $ this -> container -> make ( $ class ) ; return [ $ instance , $ method ] ; }
Build the array in order to call a class method .
11,928
protected function parseClassEvent ( $ class , $ hook ) { if ( str_contains ( $ class , '@' ) ) { return explode ( '@' , $ class ) ; } $ method = str_contains ( $ hook , '-' ) ? str_replace ( '-' , '_' , $ hook ) : $ hook ; return [ $ class , $ method ] ; }
Parse a class name and returns its name and its method .
11,929
public function make ( string $ username , string $ password , string $ email ) : User { $ this -> validate ( compact ( 'username' , 'password' , 'email' ) ) ; $ user = wp_create_user ( $ username , $ password , $ email ) ; if ( is_a ( $ user , 'WP_Error' ) ) { if ( 'existing_user_login' === $ user -> get_error_code ( ) ) { throw new DuplicateUserException ( $ user -> get_error_message ( ) ) ; } throw new UserException ( $ user -> get_error_message ( ) ) ; } return $ this -> get ( $ user ) ; }
Create a WordPress user and save it to the database .
11,930
protected function validate ( array $ data ) { $ validator = $ this -> validator -> make ( $ data , [ 'username' => 'min:6|max:60' , 'password' => 'min:6|max:255' , 'email' => 'email|max:100' ] ) ; if ( $ validator -> fails ( ) ) { $ message = sprintf ( 'Invalid user credentials. %s' , implode ( ' ' , $ validator -> errors ( ) -> all ( ) ) ) ; throw new UserException ( $ message ) ; } }
Validate user credentials .
11,931
protected function getOptions ( FieldTypeInterface $ field ) { $ options = parent :: getOptions ( $ field ) ; $ options [ 'choices' ] = $ this -> parseChoices ( $ options [ 'choices' ] ) ; return $ options ; }
Return choice field options .
11,932
protected function parseChoices ( array $ choices ) { $ items = [ ] ; foreach ( $ choices as $ key => $ value ) { if ( is_array ( $ value ) ) { $ items [ ] = [ 'key' => $ key , 'value' => '' , 'type' => 'group' ] ; $ items = array_merge ( $ items , $ this -> parseChoices ( $ value ) ) ; } else { $ items [ ] = [ 'key' => $ key , 'value' => $ value , 'type' => 'option' ] ; } } return $ items ; }
Parse field choices .
11,933
public function format ( ) : ChoiceListInterface { if ( empty ( $ this -> choices ) ) { return $ this ; } $ this -> results = $ this -> parse ( $ this -> choices ) ; return $ this ; }
Format the choices for use on output .
11,934
protected function parse ( array $ choices ) { $ items = [ ] ; foreach ( $ choices as $ key => $ value ) { if ( is_array ( $ value ) ) { $ items [ $ key ] = $ this -> parse ( $ value ) ; } else { if ( is_int ( $ key ) ) { $ label = ucfirst ( str_replace ( [ '-' , '_' ] , ' ' , $ value ) ) ; } else { $ label = $ key ; } $ items [ $ label ] = $ value ; } } return $ items ; }
Parse the choices and format them .
11,935
protected function registerBaseServiceProviders ( ) { $ this -> register ( new EventServiceProvider ( $ this ) ) ; $ this -> register ( new LogServiceProvider ( $ this ) ) ; $ this -> register ( new RouteServiceProvider ( $ this ) ) ; }
Register base service providers .
11,936
public function rootPath ( $ path = '' ) { if ( defined ( 'THEMOSIS_ROOT' ) ) { return THEMOSIS_ROOT . ( $ path ? DIRECTORY_SEPARATOR . $ path : $ path ) ; } return $ this -> webPath ( $ path ) ; }
Get the root path of the project .
11,937
public function storagePath ( $ path = '' ) { if ( defined ( 'THEMOSIS_ROOT' ) ) { return $ this -> rootPath ( 'storage' ) . ( $ path ? DIRECTORY_SEPARATOR . $ path : $ path ) ; } return $ this -> contentPath ( 'storage' ) . ( $ path ? DIRECTORY_SEPARATOR . $ path : $ path ) ; }
Get the storage directory path .
11,938
public function detectEnvironment ( Closure $ callback ) { $ args = $ _SERVER [ 'argv' ] ?? null ; return $ this [ 'env' ] = ( new EnvironmentDetector ( ) ) -> detect ( $ callback , $ args ) ; }
Detech application s current environment .
11,939
public function isDownForMaintenance ( ) { $ filePath = $ this -> wordpressPath ( '.maintenance' ) ; if ( function_exists ( 'wp_installing' ) && ! file_exists ( $ filePath ) ) { return \ wp_installing ( ) ; } return file_exists ( $ filePath ) ; }
Determine if the application is currently down for maintenance .
11,940
public function loadPlugin ( string $ filePath , string $ configPath ) { $ plugin = ( new PluginManager ( $ this , $ filePath , new ClassLoader ( ) ) ) -> load ( $ configPath ) ; $ this -> instance ( 'wp.plugin.' . $ plugin -> getHeader ( 'plugin_id' ) , $ plugin ) ; return $ plugin ; }
Bootstrap a Themosis like plugin .
11,941
public function loadPlugins ( string $ pluginsPath ) { $ directories = Collection :: make ( ( new Filesystem ( ) ) -> directories ( $ this -> mupluginsPath ( ) ) ) -> map ( function ( $ directory ) { return ltrim ( substr ( $ directory , strrpos ( $ directory , DS ) ) , '\/' ) ; } ) -> toArray ( ) ; ( new PluginsRepository ( $ this , new Filesystem ( ) , $ pluginsPath , $ this -> getCachedPluginsPath ( ) ) ) -> load ( $ directories ) ; }
Register the framework core plugin and auto - load any found mu - plugins after the framework .
11,942
public function registerConfiguredHooks ( string $ config = '' ) { if ( empty ( $ config ) ) { $ config = 'app.hooks' ; } $ hooks = Collection :: make ( $ this -> config [ $ config ] ) ; ( new HooksRepository ( $ this ) ) -> load ( $ hooks -> all ( ) ) ; }
Register a list of hookable instances .
11,943
public function registerHook ( string $ hook ) { $ instance = new $ hook ( $ this ) ; $ hooks = ( array ) $ instance -> hook ; if ( ! method_exists ( $ instance , 'register' ) ) { return ; } if ( ! empty ( $ hooks ) ) { $ this [ 'action' ] -> add ( $ hooks , [ $ instance , 'register' ] , $ instance -> priority ) ; } else { $ instance -> register ( ) ; } }
Create and register a hook instance .
11,944
public function loadTheme ( string $ dirPath , string $ configPath ) { $ theme = ( new ThemeManager ( $ this , $ dirPath , new ClassLoader ( ) ) ) -> load ( $ dirPath . '/' . trim ( $ configPath , '\/' ) ) ; $ this -> instance ( 'wp.theme' , $ theme ) ; return $ theme ; }
Load current active theme .
11,945
public function loadConfigurationFiles ( Repository $ config , $ path = '' ) { $ files = $ this -> getConfigurationFiles ( $ path ) ; foreach ( $ files as $ key => $ path ) { $ config -> set ( $ key , require $ path ) ; } return $ this ; }
Load configuration files based on given path .
11,946
protected function getConfigurationFiles ( $ path ) { $ files = [ ] ; foreach ( Finder :: create ( ) -> files ( ) -> name ( '*.php' ) -> in ( $ path ) as $ file ) { $ directory = $ this -> getNestedDirectory ( $ file , $ path ) ; $ files [ $ directory . basename ( $ file -> getRealPath ( ) , '.php' ) ] = $ file -> getRealPath ( ) ; } ksort ( $ files , SORT_NATURAL ) ; return $ files ; }
Get all configuration files .
11,947
protected function getNestedDirectory ( SplFileInfo $ file , $ path ) { $ directory = $ file -> getPath ( ) ; if ( $ nested = trim ( str_replace ( $ path , '' , $ directory ) , DIRECTORY_SEPARATOR ) ) { $ nested = str_replace ( DIRECTORY_SEPARATOR , '.' , $ nested ) . '.' ; } return $ nested ; }
Get configuration file nesting path .
11,948
public function manage ( string $ kernel , $ request ) { $ kernel = $ this -> make ( $ kernel ) ; $ response = $ kernel -> handle ( $ request ) ; $ response -> send ( ) ; $ kernel -> terminate ( $ request , $ response ) ; return $ this ; }
Handle incoming request and return a response . Abstract the implementation from the user for easy theme integration .
11,949
public function manageAdmin ( string $ kernel , $ request ) { if ( ! $ this -> isWordPressAdmin ( ) && ! $ this -> has ( 'action' ) ) { return $ this ; } $ this [ 'action' ] -> add ( 'admin_init' , $ this -> dispatchToAdmin ( $ kernel , $ request ) ) ; return $ this ; }
Handle WordPress administration incoming request . Only send response headers .
11,950
protected function dispatchToAdmin ( string $ kernel , $ request ) { return function ( ) use ( $ kernel , $ request ) { $ kernel = $ this -> make ( $ kernel ) ; $ response = $ kernel -> handle ( $ request ) ; if ( 500 <= $ response -> getStatusCode ( ) ) { $ response -> send ( ) ; } else { $ response -> sendHeaders ( ) ; } } ; }
Manage WordPress Admin Init . Handle incoming request and return a response .
11,951
public function isWordPressAdmin ( ) { if ( isset ( $ GLOBALS [ 'current_screen' ] ) && is_a ( $ GLOBALS [ 'current_screen' ] , 'WP_Screen' ) ) { return $ GLOBALS [ 'current_screen' ] -> in_admin ( ) ; } elseif ( defined ( 'WP_ADMIN' ) ) { return WP_ADMIN ; } return false ; }
Determine if we currently inside the WordPress administration .
11,952
public function outputJavascriptGlobal ( string $ name , array $ data ) { $ output = "<script type=\"text/javascript\">\n\r" ; $ output .= "/* <![CDATA[ */\n\r" ; $ output .= "var {$name} = {\n\r" ; if ( ! empty ( $ data ) && is_array ( $ data ) ) { foreach ( $ data as $ key => $ value ) { $ output .= $ key . ': ' . json_encode ( $ value ) . ",\n\r" ; } } $ output .= "};\n\r" ; $ output .= "/* ]]> */\n\r" ; $ output .= '</script>' ; return $ output ; }
Return a Javascript Global variable .
11,953
public function includes ( $ path , string $ pattern = '*.php' ) { foreach ( Finder :: create ( ) -> files ( ) -> name ( $ pattern ) -> in ( $ path ) -> sortByName ( ) as $ file ) { @ include $ file -> getRealPath ( ) ; } }
Automatically includes all . php files found on a specified directory path .
11,954
public function headers ( string $ path , array $ headers ) : array { $ data = $ this -> read ( $ path ) ; $ properties = [ ] ; foreach ( $ headers as $ field => $ regex ) { if ( preg_match ( '/^[ \t\/*#@]*' . preg_quote ( $ regex , '/' ) . ':(.*)$/mi' , $ data , $ match ) && $ match [ 1 ] ) { $ properties [ $ field ] = trim ( preg_replace ( "/\s*(?:\*\/|\?>).*/" , '' , $ match [ 1 ] ) ) ; } else { $ properties [ $ field ] = '' ; } } return $ properties ; }
Return the file headers as an associative array .
11,955
public function read ( string $ path , int $ length = 8192 ) : string { $ handle = fopen ( $ path , 'r' ) ; $ content = fread ( $ handle , $ length ) ; fclose ( $ handle ) ; return str_replace ( "\r" , "\n" , $ content ) ; }
Get a partial content of given file .
11,956
public function getView ( bool $ prefixed = false ) : string { if ( $ prefixed ) { return $ this -> getTheme ( ) . '.' . $ this -> view ; } return $ this -> view ; }
Get the section view file .
11,957
public function setViewData ( array $ data ) : SectionInterface { $ this -> data = $ data ; $ this -> data = array_merge ( $ data , [ '__section' => $ this ] ) ; return $ this ; }
Set section view data array .
11,958
public function get ( $ key = 'aliases' ) { return collect ( $ this -> getManifest ( ) ) -> flatMap ( function ( $ configuration ) use ( $ key ) { return ( array ) ( $ configuration [ $ key ] ?? [ ] ) ; } ) -> filter ( ) -> all ( ) ; }
Get manifest items by key .
11,959
public function getFields ( MetaboxInterface $ metabox , Request $ request ) : MetaboxInterface { foreach ( $ metabox -> repository ( ) -> all ( ) as $ field ) { if ( method_exists ( $ field , 'metaboxGet' ) ) { $ field -> metaboxGet ( $ request -> query ( 'post_id' ) ) ; } } return $ metabox ; }
Handle metabox initialization . Set the metabox fields value and return the metabox instance .
11,960
public function saveFields ( MetaboxInterface $ metabox , Request $ request ) : MetaboxInterface { $ post_id = $ request -> query ( 'post_id' ) ; $ data = $ this -> getMetaboxData ( collect ( $ request -> get ( 'fields' ) ) ) ; $ fields = $ metabox -> repository ( ) -> all ( ) ; $ validator = $ this -> factory -> make ( $ data , $ this -> getMetaboxRules ( $ fields ) , $ this -> getMetaboxMessages ( $ fields ) , $ this -> getMetaboxPlaceholders ( $ fields ) ) ; $ validatedData = $ validator -> valid ( ) ; foreach ( $ fields as $ field ) { $ field -> setErrorMessageBag ( $ validator -> errors ( ) ) ; if ( method_exists ( $ field , 'metaboxSave' ) ) { $ value = isset ( $ validatedData [ $ field -> getName ( ) ] ) ? $ validatedData [ $ field -> getName ( ) ] : null ; $ field -> metaboxSave ( $ value , $ post_id ) ; } else { throw new MetaboxException ( 'Unable to save [' . $ field -> getName ( ) . ']. The [metaboxSave] method is missing.' ) ; } } return $ metabox ; }
Handle metabox post meta save .
11,961
protected function getMetaboxData ( Collection $ fields ) { $ data = [ ] ; foreach ( $ fields as $ field ) { $ data [ $ field [ 'name' ] ] = $ field [ 'value' ] ; } return $ data ; }
Return the metabox data for validation .
11,962
protected function getMetaboxRules ( array $ fields ) { $ rules = [ ] ; foreach ( $ fields as $ field ) { $ rules [ $ field -> getName ( ) ] = $ field -> getOption ( 'rules' ) ; } return $ rules ; }
Return the metabox rules for validation .
11,963
protected function getMetaboxPlaceholders ( array $ fields ) { $ placeholders = [ ] ; foreach ( $ fields as $ field ) { $ placeholders [ $ field -> getName ( ) ] = $ field -> getOption ( 'placeholder' ) ; } return $ placeholders ; }
Return the metabox messages placeholders .
11,964
public function handle ( Request $ request , \ Closure $ next ) { $ route = $ request -> route ( ) ; $ response = $ next ( $ request ) ; if ( ! $ route -> hasCondition ( ) && function_exists ( 'is_user_logged_in' ) && ! is_user_logged_in ( ) ) { @ header_remove ( 'Cache-Control' ) ; @ header_remove ( 'Expires' ) ; @ header_remove ( 'Content-Type' ) ; } if ( function_exists ( 'is_user_logged_in' ) && ! is_user_logged_in ( ) ) { $ response -> setPublic ( ) ; } return $ response ; }
Cleanup response headers .
11,965
public function register ( ) { $ this -> registerFractalManager ( ) ; $ view = $ this -> app [ 'view' ] ; $ view -> addLocation ( __DIR__ . '/views' ) ; $ this -> app -> singleton ( 'form' , function ( $ app ) { return new FormFactory ( $ app [ 'validator' ] , $ app [ 'view' ] , $ app [ 'league.fractal' ] , new Factory ( ) ) ; } ) ; }
Register our form service .
11,966
public function add ( string $ handle , string $ path , array $ dependencies = [ ] , $ version = null , $ arg = null ) { if ( empty ( $ handle ) || empty ( $ path ) ) { throw new \ InvalidArgumentException ( 'The asset instance expects a handle name and a path or URL.' ) ; } return ( new Asset ( $ this -> finder -> find ( $ path ) , $ this -> action , $ this -> filter , $ this -> html ) ) -> setHandle ( $ handle ) -> setDependencies ( $ dependencies ) -> setVersion ( $ version ) -> setArgument ( $ arg ) ; }
Create and return an Asset instance .
11,967
public function load ( array $ hooks ) { if ( empty ( $ hooks ) ) { return ; } foreach ( $ hooks as $ hook ) { $ this -> app -> registerHook ( $ hook ) ; } }
Load a list of registered hookable instances .
11,968
public function reset ( Request $ request ) { $ data = new PasswordResetData ( ) ; $ form = $ this -> form ( new PasswordResetForm ( $ data ) ) ; $ form -> handleRequest ( $ request ) ; if ( $ form -> isNotValid ( ) ) { return back ( ) -> withErrors ( $ form -> errors ( ) -> all ( ) ) ; } $ response = $ this -> broker ( ) -> reset ( $ this -> credentials ( $ data ) , function ( $ user , $ password ) { $ this -> resetPassword ( $ user , $ password ) ; } ) ; return $ response == Password :: PASSWORD_RESET ? $ this -> sendResetResponse ( $ request , $ response ) : $ this -> sendResetFailedResponse ( $ request , $ response ) ; }
Handle user password reset request .
11,969
protected function credentials ( PasswordResetData $ data ) : array { return [ 'email' => $ data -> getEmail ( ) , 'password' => $ data -> getPassword ( ) , 'password_confirmation' => $ data -> getPasswordConfirmation ( ) , 'token' => $ data -> getToken ( ) ] ; }
Return password broker expected values .
11,970
public function setLabels ( array $ labels ) : TaxonomyInterface { if ( isset ( $ this -> args [ 'labels' ] ) ) { $ this -> args [ 'labels' ] = array_merge ( $ this -> args [ 'labels' ] , $ labels ) ; } else { $ this -> args [ 'labels' ] = $ labels ; } return $ this ; }
Set taxonomy labels .
11,971
public function setArguments ( array $ args ) : TaxonomyInterface { $ this -> args = array_merge ( $ this -> args , $ args ) ; return $ this ; }
Set taxonomy arguments .
11,972
public function set ( ) : TaxonomyInterface { if ( function_exists ( 'current_filter' ) && 'init' === $ hook = current_filter ( ) ) { $ this -> register ( ) ; } else { $ this -> action -> add ( 'init' , [ $ this , 'register' ] ) ; } return $ this ; }
Register the taxonomy .
11,973
public function register ( ) { register_taxonomy ( $ this -> slug , $ this -> objects , $ this -> getArguments ( ) ) ; $ this -> bind ( ) ; }
Register taxonomy hook callback .
11,974
public function setObjects ( $ objects ) : TaxonomyInterface { $ this -> objects = array_unique ( array_merge ( $ this -> objects , ( array ) $ objects ) ) ; $ this -> parseObjectsForCustomStatus ( $ this -> objects ) ; return $ this ; }
Set taxonomy objects .
11,975
protected function parseObjectsForCustomStatus ( array $ objects ) : TaxonomyInterface { foreach ( $ objects as $ object ) { if ( $ this -> container -> bound ( 'themosis.posttype.' . $ object ) ) { $ postType = $ this -> container [ 'themosis.posttype.' . $ object ] ; if ( $ postType -> hasStatus ( ) ) { $ this -> setArguments ( [ 'update_count_callback' => '_update_generic_term_count' ] ) ; break ; } } } return $ this ; }
Parse attached objects and set default update count callback .
11,976
public function display ( $ post_type , $ post ) { if ( ! is_null ( $ this -> capability ) && ! current_user_can ( $ this -> capability ) ) { return ; } if ( ! $ this -> hasTemplateForPost ( $ post ) ) { return ; } $ this -> filter -> add ( 'admin_body_class' , function ( $ classes ) { if ( false !== strrpos ( $ classes , 'themosis' ) ) { return $ classes ; } $ classes .= ' themosis' ; return $ classes ; } ) ; add_meta_box ( $ this -> getId ( ) , $ this -> getTitle ( ) , [ $ this , 'render' ] , $ this -> getScreen ( ) , $ this -> getContext ( ) , $ this -> getPriority ( ) , $ this -> getArguments ( ) ) ; }
Handle add_meta_boxes hook and display the metabox .
11,977
public function render ( \ WP_Post $ post , array $ args ) { $ args = array_merge ( $ args [ 'args' ] , [ 'metabox' => $ this , 'post' => $ post , 'screen' => $ this -> getScreen ( ) ] ) ; $ response = $ this -> handleCallback ( $ this -> getCallback ( ) , $ args ) ; if ( $ response instanceof Renderable ) { echo $ response -> render ( ) ; } elseif ( $ response instanceof Response ) { echo $ response -> getContent ( ) ; } }
Render the metabox .
11,978
public function handle ( array $ args ) { $ this -> filter -> add ( 'themosis_admin_global' , function ( $ data ) use ( $ args ) { if ( ! isset ( $ data [ 'metabox' ] ) ) { $ data [ 'metabox' ] = [ $ this -> id ] ; } elseif ( isset ( $ data [ 'metabox' ] ) ) { $ data [ 'metabox' ] [ ] = $ this -> id ; } $ data [ 'post' ] = $ args [ 'post' ] ; return $ data ; } ) ; }
Core framework metabox management . Default callback .
11,979
public function add ( $ field , SectionInterface $ section = null ) : MetaboxInterface { if ( $ field instanceof SectionInterface ) { $ section = $ field ; if ( $ section -> hasItems ( ) ) { foreach ( $ section -> getItems ( ) as $ item ) { $ item -> setOptions ( [ 'group' => $ section -> getId ( ) ] ) ; $ this -> add ( $ item , $ section ) ; } } return $ this ; } $ field -> setLocale ( $ this -> getLocale ( ) ) ; $ field -> setPrefix ( $ this -> getPrefix ( ) ) ; if ( is_null ( $ section ) ) { if ( $ this -> repository ( ) -> hasGroup ( $ field -> getOption ( 'group' ) ) ) { $ section = $ this -> repository ( ) -> getGroup ( $ field -> getOption ( 'group' ) ) ; } else { $ section = new Section ( $ field -> getOption ( 'group' ) ) ; } $ section -> addItem ( $ field ) ; } $ this -> repository ( ) -> addField ( $ field , $ section ) ; return $ this ; }
Add a field to the metabox .
11,980
public function addTranslation ( string $ key , string $ translation ) : MetaboxInterface { $ this -> l10n [ $ key ] = $ translation ; return $ this ; }
Add metabox translation .
11,981
public function setTemplate ( $ template , string $ screen = 'page' ) : MetaboxInterface { $ this -> template [ $ screen ] = ( array ) $ template ; return $ this ; }
Set the metabox template .
11,982
private function hasTemplateForPost ( \ WP_Post $ post ) : bool { $ postTemplate = get_post_meta ( $ post -> ID , '_wp_page_template' , true ) ; $ templates = isset ( $ this -> template [ $ post -> post_type ] ) ? $ this -> template [ $ post -> post_type ] : [ ] ; if ( empty ( $ templates ) ) { return true ; } return in_array ( $ postTemplate , $ templates , true ) ; }
Check if given post should use the template .
11,983
public function mapFromObjectToField ( $ data , FieldTypeInterface $ field ) { if ( ! is_object ( $ data ) ) { $ this -> triggerException ( ) ; } $ field -> setValue ( $ this -> propertyAccessor -> getValue ( $ data , $ field -> getBaseName ( ) ) ) ; }
Map data from object to field .
11,984
protected function getTokenFromRequest ( Request $ request ) { $ token = $ request -> input ( $ this -> token ) ? : $ request -> header ( $ this -> csrfHeader ) ; if ( ! $ token && $ header = $ request -> header ( $ this -> xsrfHeader ) ) { $ token = $ this -> encrypter -> decrypt ( $ header , static :: serialized ( ) ) ; } return $ token ; }
Return the CSRF token from request .
11,985
protected function generatePluginHeaders ( string $ name ) { $ description = $ this -> ask ( 'Description:' , '' ) ; $ author = $ this -> ask ( 'Author:' , 'Themosis' ) ; $ textdomain = $ this -> ask ( 'Text domain:' , trim ( $ name , '\/-_' ) ) ; $ variable = strtoupper ( $ this -> ask ( 'Domain variable:' , 'PLUGIN_TD' ) ) ; $ prefix = $ this -> ask ( 'Plugin prefix:' , str_replace ( [ '-' , ' ' ] , '_' , $ name ) ) ; $ namespace = $ this -> ask ( 'PHP Namespace:' , 'Tld\Domain\Plugin' ) ; return [ 'name' => ucwords ( str_replace ( [ '-' , '_' ] , ' ' , $ name ) ) , 'description' => $ description , 'author' => $ author , 'text_domain' => $ textdomain , 'domain_var' => $ variable , 'plugin_prefix' => $ prefix , 'plugin_namespace' => $ namespace , 'plugin_id' => $ name ] ; }
Generate plugin headers .
11,986
protected function installPlugin ( string $ name ) { $ this -> info ( 'Downloading plugin...' ) ; $ this -> files -> copy ( $ this -> option ( 'url' ) , $ this -> temp ) ; if ( true !== $ this -> zip -> open ( $ this -> temp ) ) { $ this -> error ( 'Cannot open plugin zip file.' ) ; return ; } $ this -> zip -> extractTo ( $ this -> path ( ) ) ; $ this -> zip -> close ( ) ; $ this -> files -> move ( $ this -> path ( $ this -> option ( 'dir' ) ) , $ this -> path ( $ name ) ) ; $ this -> files -> delete ( $ this -> temp ) ; }
Install the plugin .
11,987
protected function setPluginHeaders ( string $ name , array $ headers ) { $ this -> info ( 'Set plugin headers...' ) ; $ path = $ this -> path ( $ name . '/plugin-name.php' ) ; $ handle = fopen ( $ path , 'r' ) ; $ content = [ ] ; while ( ( $ line = fgets ( $ handle ) ) !== false ) { $ content [ ] = $ this -> parseLine ( $ line , $ headers ) ; } fclose ( $ handle ) ; $ this -> files -> put ( $ path , implode ( '' , $ content ) , true ) ; }
Setup the plugin headers .
11,988
protected function parseLine ( string $ line , array $ headers ) { foreach ( $ this -> headers as $ field => $ regex ) { if ( preg_match ( '/^[ \t\/*#@]*' . preg_quote ( $ regex , '/' ) . ':(.*)$/mi' , $ line , $ match ) && $ match [ 0 ] && isset ( $ headers [ $ field ] ) ) { return preg_replace ( '/:\s?+.+\s?+/' , ': ' . $ headers [ $ field ] , $ match [ 0 ] ) . "\n" ; } } return $ line ; }
Parse file header line .
11,989
protected function setPluginRootFile ( string $ name ) { $ this -> info ( 'Set plugin root file...' ) ; $ this -> files -> move ( $ this -> path ( $ name . '/plugin-name.php' ) , $ this -> path ( $ name . '/' . $ name . '.php' ) ) ; }
Set the plugin root file name .
11,990
protected function setConfigurationFile ( string $ name , array $ headers ) { $ this -> info ( 'Set plugin configuration...' ) ; $ prefix = trim ( $ headers [ 'plugin_prefix' ] , '\/_-' ) ; $ from = $ this -> path ( $ name . '/config/prefix_plugin.php' ) ; $ to = $ this -> path ( $ name . '/config/' . $ prefix . '_plugin.php' ) ; $ this -> files -> move ( $ from , $ to ) ; $ this -> replaceFileContent ( $ to , $ headers ) ; }
Set the plugin configuration file .
11,991
protected function setTranslationFile ( $ name , array $ headers ) { $ this -> info ( 'Set plugin translation file...' ) ; $ textdomain = trim ( $ headers [ 'text_domain' ] , '\/ _-' ) ; $ from = $ this -> path ( $ name . '/languages/plugin-textdomain-en_US.po' ) ; $ to = $ this -> path ( $ name . '/languages/' . $ textdomain . '-en_US.po' ) ; $ this -> files -> move ( $ from , $ to ) ; }
Set the plugin translation file .
11,992
protected function setProviders ( string $ name , array $ headers ) { $ this -> info ( 'Set default route provider...' ) ; $ this -> replaceFileContent ( $ this -> path ( $ name . '/resources/Providers/RouteServiceProvider.php' ) , $ headers ) ; }
Set the content of default route provider .
11,993
protected function replaceFileContent ( $ path , array $ headers ) { $ content = $ this -> files -> get ( $ path ) ; $ this -> files -> put ( $ path , str_replace ( [ 'DummyNamespace' , 'DummyAutoload' , 'dummy_path' ] , [ $ this -> getNamespace ( $ headers [ 'plugin_namespace' ] ) , $ this -> getAutoloadNamespace ( $ headers [ 'plugin_namespace' ] ) , $ this -> getPluginPath ( ) ] , $ content ) , true ) ; }
Replace file content with given headers values .
11,994
public function update ( array $ data ) : User { $ user = wp_update_user ( array_merge ( $ data , [ 'ID' => $ this -> ID ] ) ) ; if ( is_a ( $ user , 'WP_Error' ) ) { throw new UserException ( $ user -> get_error_message ( ) ) ; } return $ this ; }
Update user properties .
11,995
public function updateMetaData ( string $ key , string $ value ) : User { $ previous = get_user_meta ( $ this -> ID , $ key , true ) ; if ( $ previous === $ value ) { return $ this ; } $ update = update_user_meta ( $ this -> ID , $ key , $ value , $ previous ) ; if ( false === $ update ) { throw new UserException ( "Cannot update user meta data with a key of [$key]" ) ; } return $ this ; }
Update single user meta data .
11,996
protected function getDuration ( ) { $ time = $ this -> option ( 'time' ) ; return is_numeric ( $ time ) && $ time > 0 ? ( int ) ( ( time ( ) - ( 10 * 60 ) ) + $ time ) : 'time()' ; }
Return the maintenance duration .
11,997
public function addField ( FieldTypeInterface $ field , SectionInterface $ group ) : FieldsRepositoryInterface { $ this -> fields [ $ field -> getBaseName ( ) ] = $ field ; $ this -> groups [ $ group -> getId ( ) ] = $ group ; return $ this ; }
Add a field to the form instance .
11,998
public function getField ( string $ name = '' , string $ group = 'default' ) { $ section = $ this -> groups [ $ group ] ; $ foundItems = array_filter ( $ section -> getItems ( ) , function ( FieldTypeInterface $ item ) use ( $ name ) { return $ name === $ item -> getBaseName ( ) ; } ) ; return ! empty ( $ foundItems ) ? array_shift ( $ foundItems ) : [ ] ; }
Return the defined field instance based on its basename property . If not set return all fields from the default group .
11,999
public function make ( string $ slug , string $ title ) : PageInterface { $ view = ( new PageView ( $ this -> view ) ) -> setTheme ( 'themosis.pages' ) -> setLayout ( 'default' ) -> setView ( 'page' ) ; $ page = new Page ( $ this -> action , $ this -> filter , $ view , new PageSettingsRepository ( ) , $ this -> validator ) ; $ page -> setSlug ( $ slug ) -> setTitle ( $ title ) -> setMenu ( $ title ) ; $ this -> view -> getContainer ( ) -> instance ( $ this -> prefix . '.' . $ page -> getSlug ( ) , $ page ) ; return $ page ; }
Make a new page instance .