 Okay. Hello. So how many of you are PHP developers? Nice. How many of you are using WPC-Li? Awesome. Do you use WPC-Li together with PHP? Okay. Cool. So let's try something new today. If you ever see me doing any WPC-Li, you saw me using WPEval command. So I'm going to just go to my local environment. This is freshly installed. Oh, no caps. Okay. Right. This is freshly installed WordPress. And you probably saw me doing this, WPEval. And then echo WP login URL. And you get this. And this is annoying because you can't use this. It's not in a new line. So what you do to make that better, you just add a new line. And we don't want errors right away. So here is usable login URL. Okay. So that's what you probably saw me doing. There are other ways to do it with WPC-Li. So evil is obviously command to execute PHP function that I've been using more than I care to admit on servers. So when you have a function you want to execute just once, you go to server and you do use evil. And it's all jolly. You can use also shell. Has anyone used shell before? Okay. Cool. So what is shell? Shell is PHP console open in your terminal. So here I can just do WP login URL. And it will give me strings. So I can do other things. You can get things from database. You can, you just run something to see what will it return on that specific WordPress install. I can also say print R and get, for example, users. It has to be a function, right? And you get older. So you see how it happens? You can use it as PHP. It's not very readable though. But it is useful. So you can use, you can do the same thing with WPC-Li and with shell depending on what you're more comfortable with. So for example, if I want to get the option, WP option at, for example, blog name, I can have it this way. Or I can say WP shell and say get option, blog name. And I get it. I can update it the same way here. So I can update it with PHP. I can say update, option. And you see I know this by heart because I practiced for this workshop. So yeah, I don't usually know all the functions by heart. So I can say blog name. That is the option name. And the second parameter is my new value, which can be WPC-Li, for example, too. And now we have error. Thank you. Yeah, because, right, I'll try to copy paste it, which is always a fun thing to do in terminal, right? And now apparently I've changed it. So let me get out of the shell. Let me open this install. And you see it's updated WPC-Li, too. I can do that with WPC-Li, like option, update. And then we have blog name. I hope it's, no, I hope it's like that. I don't know. We'll see if we get error. Oh, it's updated. So when we go back, see, depending what you're more comfortable with PHP or WPC-Li, you can do these things. So that was enough fun and play. We have work to do. I'm just kidding. Everything is fun and play here. Yeah. So this is something I really, really love. And it's a command evil file. Have anyone been using this? Nice. I see just hosting people using evil file. Okay, so evil command will execute PHP code. Evil file will execute, you guessed, PHP file. And this is where the fun begins. So let's first see what this function offer us to do. It says evil file, file, and then some kind of arguments and skip WordPress. So this is a nice thing if you want to execute just file, but you want to skip loading all the WordPress. And maybe you have some error there. You don't want to run on that error and stop the execution. You can do that as well. And what are the args? It says one or more arguments to pass to the file. They are placed in args variable. And there is no example here. But usually WordPress, WPCI commands have examples. And let's see what's happening there. Because now we know there are parameters that we can use, arguments, but there's no... So what you do, this is what a good open source citizen do. You go to source code and check evil file command. And you see here is example. So it's not visible. So let's see this. When I was just testing out, I didn't know how do I put this? Do I name it? Do I, you know, how do you use these arguments? So here we see in this example, it's value one, value two, and it's all in args array. So because there is an example that is very useful and it's not visible, that is a bug. So you see this three days ago, I opened PR. So that's what you do when you are a good open source citizen. I just updated like 20 minutes ago. So I hope it will get committed soon and merge. Okay. So let's create PHP file and see how that works. So how you do it in terminal, you say touch. And let's say W C E U PHP. For example, now we created this file. Let me find it. You see it under... Okay. Thank you so much. So let's open it in code. I usually do things in terminal, but now we are going to write serious PHP. So talking about serious PHP, we are going to do echo. Hello. Word can pure up, you rock. For example, this is the most serious PHP. And how you execute it. So let's delete all of this here. I don't know why this happened. Actually, I do know, but it's not what I wanted. I'm going to just type the command. So evo file and the name of the file E C E U PHP. Let's go back to terminal. And when we execute it, oh, hello. This is great. But again, we have this in the same line. And that is so annoying. And luckily, we can create the professional messages for terminal as WPC LI is doing. So let me introduce to you internal API for WPC LI. So you can find it in make WordPress.org CLI handbook. And then we go to references and internal API. And here you can register your hooks and commands. And you can do stuff with output. So this is what we want to do now. Let's take this log. What log does is it's just give you a nice message. Nicely formatted message here. And we can remove that one. Okay. Now, this is important always to follow coding standards. So you can and should allow every string for translation. Now, if we run it again, yay, we rock. Now let's pass some parameters to this file. The documentation says there is a args array that we will get it. So let's see how that will look like. I'm just going to print it here. And as you saw, there was just value one, value two. So if I say WP evil file, and then file name, and I can say maybe, I don't know, the workshop is great. There has to be a space. Okay. So you see what happens here. This is array and you get every item, every word is one item in array. So I can do here something like, you know what we could try? I haven't tried this. So maybe we break things. But we can try to, instead of the actual hard coded words, to find some value from the WordPress install. For example, if we want to get the site name, we can type WPC line, but we can also use the get option, blog name, right? So how you do it when you have already one command and you want to use another command as a parameter for this command, you use dollar sign and then brackets and inside, you put a command. So let's say WP option, get, blog name. In theory, this should work, but let's see what's going to happen. Oh, my God. This is beautiful. So now I can say here, I'm going to remove this. I can say, hello, WorldCamp Europe. This site name is and I'm going to say args and zero. So whatever word I pass first, it will be, it should show up here. So I'm just going to repeat this. Oh, yay. I hope you see the value of this. But you probably don't want always to just pass argument. Sometimes you want to, but maybe not always. Maybe you just want to write PHP file and just run the functionality. And the use case for that would be, for example, I saw many times people asking, can I export WooCommerce products, but not everything, just the name and the price and maybe something, some argument. So you have to do a lot of custom coding to do that and then you test it and whatever. You can do it here. I'm not going to install WooCommerce for this. We are going to use something just similar to that. But any kind of entity, you can actually just create a functionality to export to SCV file and then import to some other WordPress install. Now, don't get me wrong. You can do this with WPCLI as well. So let's try users. Like WP user list. You will get all users, right? We have only one. And I hate to work with that little data. So I'm going to say WP user generate. And look at it go. We have 100 users now. WP user list. Yay. So what we can do is WP user list and then do this. Users, CSV. Because default output, actually default output you saw was a table. So what we will get now, here you see users, CSV. Let me open that with... Okay, let's go like this. We save them here. Here is users. So this is what you get. Default output is with these column names. And this is what you get. Display name, user email. This is by default. You can actually make those fields customized. So maybe I want... Let me see. I need help here. And this is great because you can get all the help from terminal. So I can say fields and then fields. You see you can define format as well. And these are the fields that you can use. So let me say I want to do ID, user login, email. And I want password. Here, user pass. Okay. Because that is so safe to do. So fields. Okay. It's going to be ID, then user email, then user... What did we say? Login. And user pass. And then I want to put it in user's CSV. Now let's open it. You see? It's different. So what we can do with PHP, maybe you don't want to type all the time, all that parameters and fields. Maybe you just want to execute because you're lazy like me. And maybe you are more comfortable with PHP. So we can start here with users equals get users. So you always want to go to documentation to see get users, to see what parameters are there, its array of arguments, and then we can do many things here. What I want to do is... I want to show you this. So when you get users, you get a lot of input there. So let's just print users. What was that? WP, evil file. This one. Yeah, okay. So you get here a lot of it. We don't want all of it. What we wanted was just those fields, right? So we will go to find fields. Here we have... These are the fields we want to get. So it's fields, field is array. No, this should be... Sorry, array. So from fields we wanted ID and then user, login, user. What was their email user? And by coding standards, every array last item has to have a comma. Okay. So now when we run users again, you get far less. Now I don't know why is this happening. Maybe I need to update something. But I get double ID and then I remove it from the code. I get no ID at all. That started happening yesterday. Maybe something for debugging. Maybe I just really need to update my system. But this is what you get. So now we get this much better manageable data. And now if we want to put it into CSV file, we can use pure PHP for that. And there is a function that I forgot. What's the name of the... I didn't. I'm just pretending. So you know it's okay to forget it. So let's write CSV to PHP. You see this is the first result. Always go to official, manual. So it's f, put, file, puts CSV. And there is example how to do it. So you need two additional functions for that. You need to open file. Then you run a loop through the array of data. You put data in that file. Every array is one row. And then you close the file. So let's just copy all of this. Actually, we don't need the list. We need only this. Is everyone following correctly? Everything's fine? Cool. If you need any help, yell. I don't have cookies today, but I might be able to help. So I want to rename this to be users, but from PHP. And you see this other parameter is W that means write. But what that actually means is the pointer, every time you run it, the pointer will be at the beginning of file. So you will lose all the data every time you run it. It will overwrite it. There is another parameter you can use if you want to append. And it is A for append. And as always, I suggest going through documentation and just see what's there available. So here you see all the parameters that you can use. And maybe they fit better for your specific case. So we have the array of users. And then we are going to say for each user as user. And now we put user. And I'm going to comment that. So we didn't take a good look at this one. So what it says here first parameter is stream. So that is the file, the PHP file that we are, the SC or any file that you want to put your data in. Then we have fields. And that is an array of string. And this will give us error. Does anybody know why? Running this function like this. Do you see this? Yeah. We are getting from get users, we are getting array of objects. And this function wants array of strings. So we need to, every user that we get here, we need to convert this object into a array of strings. So we're going to say user array equals array. I'm going to copy this from here. Okay. I'm going to do it to select all of that. Copy and say it's user. And like this. So now we have a nice array. And we are going to change this here. Okay. And then we have, we close this and it should work. Let's see if it works. We don't have any feedback. But we have user's PHP file here. User's PHP CSV. And when we open it, we have everything. And ID is not doubled. So that's good. But we don't have these table names. So that is the difference. If you do it this way, maybe you don't need it. Maybe it's, I don't know. Maybe there are ways to add it. But you can, as you saw, easily do it with WPCLi if you need those column names. So this is good. But I want to have some kind of feedback. I feel uncomfortable just, you know, running it and kind of let's hope it works. So you can do this. Just add it here. And let's say this user, user nice. No, we don't have, we don't have nice name. We have login, login, rock. So for every user, we should get yay, they rock. Okay. So now we have exported that. But maybe, maybe we want to have a nice table. Like WPCLi is doing table. And we can do that with internal API. What we are going to do here is see this? Format items. And this is how it looks like. So we have items, array of items. So this is array of arrays. And then we have table. We can choose if that's going to be, where does it say all the parameters? It doesn't. But everything that you saw. So output can be JSON or CSV or whatever you want. We want to do table. And then we have this array of items key value. This is actually the column title. So I'm going to just copy this, paste it here. Table items. So what we need now is array of arrays. We need an array that will catch every user array we created. So I'm going to start above the loop. And I'm going to say items equals empty array. And then I'm going to add here items. And just add the user array to it. And we have items. Okay. And now we need these names for columns. So we need these. And I'm going to just get them out of here. And let's say fields. So this is our array of items. And here, fields. Now my Visual Studio code is yelling. There's some errors. But I don't think there are. So here we have the right. You can also add here a way to execute the WPCLI command from PHP file. So I'm going to delete all these users except for my own original users. So I'm going to say WPCUserDelete. Now the parameter here is ID. So what we can do is say ID2, ID3, ID4. But that's not how you use terminal. Because when you have terminal, you are the most powerful person on server, right? So what you want to do, you want to combine it with other tools. Again, I want to execute. So when I want to loop through all the users, I use user list, right? And I need here parameter ID. So I need to loop through users, get ID for every each of them except for my own. Because I want my administrator user to stay there. So let me see user list help. Always use help. This is what we do when we want to return a field, a specific field. We are going to say ID, right? So this is control output by one or more arguments of user queries. So I want just ID to be returned. But also I want to exclude my own user. And I don't see here option to exclude. And that's actually this one. Control output by one or more arguments. So you can choose which argument will be here. And this is what I have. Exclude. So I need ID of a user to exclude. Nice. We can say WP user list. Now I said I want field that be held. It's a bugger that you have to type it correctly. Field equals ID. So this, yeah. But it's PHP as well. So that's not excuse not to use terminal. So field ID, this is what I want to be returned. Just ID of every user. And my specific field that I wanted to add is equals ID of the user. And I want to exclude myself, which is I think one. So let's just run it here. Okay. Nice. And let's check if, yeah. One is excluded. So now I have my loop. And I can use this as a parameter for the delete. WP user delete. And as I said, when you want to execute another command as a parameter of one command, you use dollar and brackets like this. Yeah. Proceed. And this is removed. User list. So now I have only my own user. And I can say here at the beginning, let's go to internal API. Input execution. We want to run command. So I just deleted users because I want to create new users inside of the file. Not because it's very useful in this case, but because you might want to execute something before running. So here is the usage. You have options. And then you can try without options. I don't know if it's doable. If we will get some kind of error. I don't know. We still didn't have any serious error. And I'm kind of sad because of that. So return true. Okay. That's fine. Parse JSON. I don't know. Yeah. We can use JSON. Launch false. Yeah. Exit error. Okay. That's fine. And command args. So you can add global parameters to this like additional args. I'm not going to use this. And as you see, you omit wp when you do it this way. So you just have a command. And I'm going to say user generate because I want this. Oh, and I said we are going to try without options. Let's see if we can break it. We didn't break it. So sad. Yeah. But now we have different IDs for these users. And they all rock. Let's open the CSV file. And yeah, we have the different IDs. So we generated users and we exported them to CSV file. What we can also do is if you need, we can change this to append. And now run it again. And when we open, we should have right. We have appended. So we've saved the previous ones and we appended new ones. Now, we have only 16 minutes left. I wanted to do, yeah, six minutes left. So we don't have, I actually wanted to add a script to import users as well. So I'm just going to show you quickly how to do it. So you have put CSV, but you also have on this documentation page, you will get CSV. And this is how you would do it in another WordPress install. You would do this. So you will open. Again, you need open and close files. So you will open this file. Data is where your users are stored. So you will not need this for loop. You will just need data is one, one item in data is one user. So you can just add them. And you can use insert user or, you know, whatever PHP function, a WordPress function. And you would get all the users inside of your new install. And that's how you do WP CLI and PHP together. And there are a lot of more things, but we don't have enough time. And oh, yeah, I have last slide. So my name is Mila Natsap. I'm WordPress engineer at XWP and the loudest member of the documentation team. You can find me on these places. I'm also classical musician. So if you want to talk about that, yeah, it's fine. You can find me on these places or you can find me here in a hall. I'll be here two days for any questions, any discussions, any proposals. If you have a hosting, you have server, you want it broken. I'm there. And thank you so much for your time. We do have time for one question. If there's any from the floor. Yep. I was wondering how the performance of the WP CLI is compared to the WP JSON API. Is it better? Is it worse? I don't know. I didn't measure it. I'm probably not the right person for that. I do care about breaking stuff and finding how things work. But if you really want to talk about performance, we have in at XWP a lot of people who really knows that. So I'm just breaking stuff. They're fixing. Yeah, yeah, yeah, yeah. We can break more. So I personally love it more. Yeah. And he's the person actually you can talk to. It's Mike from XWP. Just one more, one more before we end. Any more? No? Yes, no? Okay, fantastic. Thank you. That was amazing, Milan. Thank you so much. And before you go, there's a token of appreciation from the team. Yeah. Thank you. Thank you so much. Thank you. All right.